• Post Reply Bookmark Topic Watch Topic
  • New Topic

Read data from file !  RSS feed

 
Moniphal Say
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I want to read data from file. I want to read 1 line at a time from file. But i dont know how to do it, i only can read with the size of buffer.
this is my code :


public void readFile(String filename){
try{
// try to start reading
Reader in = new FileReader(filename);
char[] buff = new char[1];
int nch;
try{
while ((nch = in.read(buff, 0, buff.length)) != -1) {
addNodes(buff,""); // my function for add node to Jtree
}
}catch (IOException e) {
System.err.println(e.toString());
}
}catch (FileNotFoundException e){
System.err.println(e.toString());
}
}


with this code, i can not read 1 line at a time, it is depend on the size of buffer ! so please anyidea how to read 1 line at a time from file !

Regards,
Moniphal
 
Sonny Gill
Ranch Hand
Posts: 1211
IntelliJ IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at java.io.BufferedReader.
http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html

Cheers
 
Marcus Pant
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try {
BufferedReader in = new BufferedReader(new FileReader("infilename"));
String str;
while ((str = in.readLine()) != null) {
processWhateverYouWant(str);
}
in.close();
} catch (IOException e) {
...
}
 
Moniphal Say
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to both of you for your help , Now i can do it. thanks alots

Regards,
Moniphal
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!