Connecting Tech Pros Worldwide Forums | Help | Site Map

Read a file

Newbie
 
Join Date: Sep 2007
Posts: 16
#1: Oct 16 '07
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

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#2: Oct 16 '07

re: Read a file


Quote:

Originally Posted by Jromero

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
Newbie
 
Join Date: Sep 2007
Posts: 16
#3: Oct 16 '07

re: Read a file


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
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#4: Oct 16 '07

re: Read a file


Look, have look at "ObjectInputStream","ObjectOutputStream" and "Serialization" in java.
Have a look through Google.
Any further problem, please post here.

Debasis Jana
Newbie
 
Join Date: Sep 2007
Posts: 16
#5: Oct 16 '07

re: Read a file


I still don't get it please help me
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#6: Oct 16 '07

re: Read a file


Quote:

Originally Posted by Jromero

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
Newbie
 
Join Date: Sep 2007
Posts: 16
#7: Oct 16 '07

re: Read a file


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
dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#8: Oct 16 '07

re: Read a file


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
Newbie
 
Join Date: Sep 2007
Posts: 16
#9: Oct 17 '07

re: Read a file


I am still having problems I don't where to put the object stream
Reply