Operation on a txt file..
1st is to obtain data from this text file n simply show in the console n then to show the information separated by commas under the columns of name: roll number: class: phone number: course:
secondly it's to tell that how much characters, integers, blank spaces are in the .txt file...
FILE DATA:
andre neil, 211, IT, 04130001701, BSCS
David miller, 219, CS, 0234000201, BSCS
Johnson, 344, Maths, 0523400000478, BS(Maths)
Code JAVA:
import java.util.*;
import javax.swing.*;
import java.io.*;
public class TestStudent{
public static void main(String args[]){
FileReader fr = null;
BufferedReader br = null;
String[] tokens=null;
try{
fr = new FileReader("student.txt");
br = new BufferedReader(fr);
String line = br.readLine();
while (line !=null)
{
tokens= line.split(",");
System.out.println(line);
line = br.readLine();
}
br.close();
fr.close();
}
catch(IOException ioex){
System.out.println(ioex);
}
System.out.println("student no.1");
Student s1 =new Student(tokens[0],tokens[1], tokens[2], tokens[3], tokens[4]);
s1.print();
}
it's giving me just the last student's data
student class is another file just to show the columns headings accordingly...
Re: Operation on a txt file..
The problem is when you read the data into tokens[].
Code Java:
while (line !=null)
{
tokens= line.split(",");
System.out.println(line);
line = br.readLine();
}
'Tokens' can only contain data of just one student.
So in the loop 'tokens' is overwritten with other data all the time.