472,328 Members | 967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 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 2363
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...
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...
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...
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...
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));...
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...
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...
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...
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...
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...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.