473,379 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Read a file

16
My code
[

public class Student{
private String lastName;
private String firstName;
private String middleName;
private int sid;
private double units;
private String Programs;


public Student(){
this( "", "", "", 0, 0.0, "");
}

public Student (String lastName, String firstName, String middleName, int sid, double units, String Programs )
{
//setLastName(lastName);
setFirstName(firstName);
setMiddleName(middleName);
setsid(sid);
setunits(units);
setPrograms(Programs);

}

public void setLastName(String lastName){
lastName = lastName;
}

public String getLastName(){
return lastName;
}

public void setFirstName(String firstName){
firstName = firstName;
}

public String getFirstName(){
return firstName;
}

public void setMiddleName(String middleName){
middleName = middleName;
}

public String getMiddleName(){
return middleName;
}

public void setsid(int sid){
sid = sid;
}
public int getsid(){
return sid;
}


public void setunits(double units){
units = units;
}


public double getunits()
{
return units;
}


public void setPrograms(String Programs){
Programs = Programs;
}

public String getPrograms(){
return Programs;
}

}

]
Another class
[

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Roaster {

Student st[] = new Student[100];

/* public static void copyLines() throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;

try {
inputStream =
new BufferedReader(new FileReader("roaster.txt"));

for(i=0;i<no.oflines;i++)
{
addstudent(fir,
}
outputStream =
new PrintWriter(new FileWriter("characteroutput.txt"));

String l;
while ((l = inputStream.readLine()) != null) {

outputStream.println(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}*/

public void addStudentfromFile() throws IOException
{
Scanner inputStream = null;
try {
inputStream =
new Scanner(new File("roaster.txt"));
int noofstu = 1;

while (inputStream.hasNext()) {

String temp = inputStream.next();
String[] name = null;
name = temp.split(",");
/* System.out.println(name[0]);
System.out.println(name[1]);
System.out.println(inputStream.next());

System.out.println(inputStream.nextInt());

System.out.println(inputStream.nextDouble());
System.out.println(inputStream.next());*/
st[noofstu-1] = new Student(name[0], name[1], inputStream.next(),inputStream.nextInt() , inputStream.nextDouble(),inputStream.next());
noofstu++;
}
} finally {
if (inputStream != null) {
inputStream.close();
}

}

}

public int getStudentno(){
//getStudent = new Student;
return st.length;

}


public static void main(String[] args) {


}
}
]
Another class for testing
[
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;

public class RoasterTest {

public static void main (String[] args) throws Exception{
File RoasterTest = new File( "roaster.txt" );
if ( RoasterTest.exists() )
{
// Create the buffered reader for reading the file
BufferedReader inFile = new BufferedReader( new FileReader( RoasterTest ) );



// Get the first line of the file
String line = inFile.readLine();

// If line is not end of file continue
while ( line != null )
{
// Create a StringTokenizer with a colon sign as a delimiter
java.util.StringTokenizer st =
new java.util.StringTokenizer( line, ":" );

// Display the content of the first token
System.out.print( " Name: " + st.nextToken() );

// Display the total number of tokens remaining this string
int numScores = st.countTokens();

// Initialize the sum to zero
int sum = 0;

// Get each score, add it to the sum and print it
for ( int i=1; i <= numScores; i++ )
{
int score = Integer.parseInt( st.nextToken() );
sum += score;
}

// Display the average score for this student
System.out.println( " average = " + sum/numScores );

// Read next line of the file
line = inFile.readLine();

} // end while not at end of file

// Close the BufferedReader
inFile.close();

// try {
// RoasterTest.clone();
// } catch (IOException ioe) {

} // end if the grade file doesn't exist

}

}

]

My test class does not read student class or roaster class .. I know I am missing something but I am loss
Oct 16 '07 #1
8 2434
dmjpro
2,476 2GB
My code
[

public class Student{
private String lastName;
private String firstName;
private String middleName;
private int sid;
private double units;
private String Programs;


public Student(){
this( "", "", "", 0, 0.0, "");
}

public Student (String lastName, String firstName, String middleName, int sid, double units, String Programs )
{
//setLastName(lastName);
setFirstName(firstName);
setMiddleName(middleName);
setsid(sid);
setunits(units);
setPrograms(Programs);

}

public void setLastName(String lastName){
lastName = lastName;
}

public String getLastName(){
return lastName;
}

public void setFirstName(String firstName){
firstName = firstName;
}

public String getFirstName(){
return firstName;
}

public void setMiddleName(String middleName){
middleName = middleName;
}

public String getMiddleName(){
return middleName;
}

public void setsid(int sid){
sid = sid;
}
public int getsid(){
return sid;
}


public void setunits(double units){
units = units;
}


public double getunits()
{
return units;
}


public void setPrograms(String Programs){
Programs = Programs;
}

public String getPrograms(){
return Programs;
}

}

]
Another class
[

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Roaster {

Student st[] = new Student[100];

/* public static void copyLines() throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;

try {
inputStream =
new BufferedReader(new FileReader("roaster.txt"));

for(i=0;i<no.oflines;i++)
{
addstudent(fir,
}
outputStream =
new PrintWriter(new FileWriter("characteroutput.txt"));

String l;
while ((l = inputStream.readLine()) != null) {

outputStream.println(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}*/

public void addStudentfromFile() throws IOException
{
Scanner inputStream = null;
try {
inputStream =
new Scanner(new File("roaster.txt"));
int noofstu = 1;

while (inputStream.hasNext()) {

String temp = inputStream.next();
String[] name = null;
name = temp.split(",");
/* System.out.println(name[0]);
System.out.println(name[1]);
System.out.println(inputStream.next());

System.out.println(inputStream.nextInt());

System.out.println(inputStream.nextDouble());
System.out.println(inputStream.next());*/
st[noofstu-1] = new Student(name[0], name[1], inputStream.next(),inputStream.nextInt() , inputStream.nextDouble(),inputStream.next());
noofstu++;
}
} finally {
if (inputStream != null) {
inputStream.close();
}

}

}

public int getStudentno(){
//getStudent = new Student;
return st.length;

}


public static void main(String[] args) {


}
}
]
Another class for testing
[
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;

public class RoasterTest {

public static void main (String[] args) throws Exception{
File RoasterTest = new File( "roaster.txt" );
if ( RoasterTest.exists() )
{
// Create the buffered reader for reading the file
BufferedReader inFile = new BufferedReader( new FileReader( RoasterTest ) );



// Get the first line of the file
String line = inFile.readLine();

// If line is not end of file continue
while ( line != null )
{
// Create a StringTokenizer with a colon sign as a delimiter
java.util.StringTokenizer st =
new java.util.StringTokenizer( line, ":" );

// Display the content of the first token
System.out.print( " Name: " + st.nextToken() );

// Display the total number of tokens remaining this string
int numScores = st.countTokens();

// Initialize the sum to zero
int sum = 0;

// Get each score, add it to the sum and print it
for ( int i=1; i <= numScores; i++ )
{
int score = Integer.parseInt( st.nextToken() );
sum += score;
}

// Display the average score for this student
System.out.println( " average = " + sum/numScores );

// Read next line of the file
line = inFile.readLine();

} // end while not at end of file

// Close the BufferedReader
inFile.close();

// try {
// RoasterTest.clone();
// } catch (IOException ioe) {

} // end if the grade file doesn't exist

}

}

]

My test class does not read student class or roaster class .. I know I am missing something but I am loss
Use Code Tags.
Please post the file content.

Debasis Jana
Oct 16 '07 #2
Jromero
16
My code
[

public class Student{
private String lastName;
private String firstName;
private String middleName;
private int sid;
private double units;
private String Programs;


public Student(){
this( "", "", "", 0, 0.0, "");
}

public Student (String lastName, String firstName, String middleName, int sid, double units, String Programs )
{
//setLastName(lastName);
setFirstName(firstName);
setMiddleName(middleName);
setsid(sid);
setunits(units);
setPrograms(Programs);

}

public void setLastName(String lastName){
lastName = lastName;
}

public String getLastName(){
return lastName;
}

public void setFirstName(String firstName){
firstName = firstName;
}

public String getFirstName(){
return firstName;
}

public void setMiddleName(String middleName){
middleName = middleName;
}

public String getMiddleName(){
return middleName;
}

public void setsid(int sid){
sid = sid;
}
public int getsid(){
return sid;
}


public void setunits(double units){
units = units;
}


public double getunits()
{
return units;
}


public void setPrograms(String Programs){
Programs = Programs;
}

public String getPrograms(){
return Programs;
}

}

]
Another class
[

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Roaster {

Student st[] = new Student[100];

/* public static void copyLines() throws IOException {
BufferedReader inputStream = null;
PrintWriter outputStream = null;

try {
inputStream =
new BufferedReader(new FileReader("roaster.txt"));

for(i=0;i<no.oflines;i++)
{
addstudent(fir,
}
outputStream =
new PrintWriter(new FileWriter("characteroutput.txt"));

String l;
while ((l = inputStream.readLine()) != null) {

outputStream.println(l);
}
} finally {
if (inputStream != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}*/

public void addStudentfromFile() throws IOException
{
Scanner inputStream = null;
try {
inputStream =
new Scanner(new File("roaster.txt"));
int noofstu = 1;

while (inputStream.hasNext()) {

String temp = inputStream.next();
String[] name = null;
name = temp.split(",");
/* System.out.println(name[0]);
System.out.println(name[1]);
System.out.println(inputStream.next());

System.out.println(inputStream.nextInt());

System.out.println(inputStream.nextDouble());
System.out.println(inputStream.next());*/
st[noofstu-1] = new Student(name[0], name[1], inputStream.next(),inputStream.nextInt() , inputStream.nextDouble(),inputStream.next());
noofstu++;
}
} finally {
if (inputStream != null) {
inputStream.close();
}

}

}

public int getStudentno(){
//getStudent = new Student;
return st.length;

}


public static void main(String[] args) {


}
}

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;

public class RoasterTest {

public static void main (String[] args) throws Exception{
File RoasterTest = new File( "roaster.txt" );
if ( RoasterTest.exists() )
{
// Create the buffered reader for reading the file
BufferedReader inFile = new BufferedReader( new FileReader( RoasterTest ) );



// Get the first line of the file
String line = inFile.readLine();

// If line is not end of file continue
while ( line != null )
{
// Create a StringTokenizer with a colon sign as a delimiter
java.util.StringTokenizer st =
new java.util.StringTokenizer( line, ":" );

// Display the content of the first token
System.out.print( " Name: " + st.nextToken() );

// Display the total number of tokens remaining this string
int numScores = st.countTokens();

// Initialize the sum to zero
int sum = 0;

// Get each score, add it to the sum and print it
for ( int i=1; i <= numScores; i++ )
{
int score = Integer.parseInt( st.nextToken() );
sum += score;
}

// Display the average score for this student
System.out.println( " average = " + sum/numScores );

// Read next line of the file
line = inFile.readLine();

} // end while not at end of file

// Close the BufferedReader
inFile.close();

// try {
// RoasterTest.clone();
// } catch (IOException ioe) {

} // end if the grade file doesn't exist

}

}

]

This is my code
Oct 16 '07 #3
dmjpro
2,476 2GB
Look, have look at "ObjectInputStream","ObjectOutputStream" and "Serialization" in java.
Have a look through Google.
Any further problem, please post here.

Debasis Jana
Oct 16 '07 #4
Jromero
16
I still don't get it please help me
Oct 16 '07 #5
dmjpro
2,476 2GB
I still don't get it please help me
Look,
"ObjectInputStream" for object reading from a stream,
and "ObjectOutputStream" for object writing for a strean.
Now the Object should implement "Serializable" interface.

Have a look at this sample code ...............

Expand|Select|Wrap|Line Numbers
  1. ObjectOutputStream o = new OutjectOutputStream(new FileOutputStream("test.dat"));
  2. Student s = new Student(/*Some data goes here*/);
  3. o.writeObject(s);
  4. .
  5. .
  6. ObjectInputStream i = new ObjectInputStream(new FileInputStream("test.dat"));
  7. Student _s = (Student)i.readObject();
  8. //now access the _s object.
  9.  
Your Student class should be like this .......
Expand|Select|Wrap|Line Numbers
  1. class Student implements Serializable
  2. {
  3.  //your code goes here
  4. }
  5.  
Debasis Jana
Oct 16 '07 #6
Jromero
16
The
Code[
ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("roaster.txt"));
Student s = new Student("String lastName, String firstName, String middleName, int sid, double units, String Programs", null, null, 0, 0, null);
o.writeObject(s);

ObjectInputStream i = new ObjectInputStream(new FileInputStream("roaster.txt"));
Student _s = (Student)i.readObject();
// now access the _s object.

]
Goes in Roaster class
Oct 16 '07 #7
dmjpro
2,476 2GB
You got your code worked?

Actually I forgot to close the streams.
See the code carefully.
Expand|Select|Wrap|Line Numbers
  1. ObjectOutputStream o = new OutjectOutputStream(new FileOutputStream("test.dat"));
  2. Student s = new Student(/*Some data goes here*/);
  3. o.writeObject(s);
  4. o.close();
  5. .
  6. .
  7. ObjectInputStream i = new ObjectInputStream(new FileInputStream("test.dat"));
  8. Student _s = (Student)i.readObject();
  9. i.close();
  10. //now access the _s object.
  11.  
Your Student class should be like this .......
Expand|Select|Wrap|Line Numbers
  1. class Student implements Serializable
  2. {
  3.  //your code goes here
  4. }
  5.  

Debasis Jana
Oct 16 '07 #8
Jromero
16
I am still having problems I don't where to put the object stream
Oct 17 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Yang Li Ke | last post by:
Hi guys! I have some datas that I must check everytime a visitor comes to my site What is better to do: 1- Read data from a file or 2- Read data from a mysql db Thank you
10
by: ZafT | last post by:
Thanks in advance for any tips that might get me going in the right direction. I am working on a simple exercise for school that is supposed to use read to read a file (about 10 MB). I am...
1
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
5
by: lovecreatesbea... | last post by:
The condition at line 31 is added to check if the program finished to read the whole file. Is it needed and correct? Thank you. #include <fstream> #include <iostream> #include <string> using...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
9
by: flebber | last post by:
I was working at creating a simple program that would read the content of a playlist file( in this case *.k3b") and write it out . the compressed "*.k3b" file has two file and the one I was trying...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.