Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How can i read data from file  RSS feed

 
Ravi Mandalapu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to I/O's.
I want to read data from file and display that data on form. If you have any sample code....
Any help would be greatly appeciate.
Ravi
 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
the following code will read a file and return the files data a line at a time. I am not sure how you want to process your data, but this part will at least show you how to open a file and access the data a String at a time. bwin.readline() returns a null when it gets to the end of the file.
You can also use just FileReader and do in.read to return a character at a time if that better suits your purpose. (I can send another bit of code showing that if you are interested.)
Hope this helps,
Kim

public void readFile() throws IOException {
File inputFile = new File("C:\\myfile.txt");
FileReader in = new FileReader(inputFile); BufferedReader bwin = new BufferedReader(in);
String line;
while ((line=bwin.readLine()) !=null){
// code here to parse line and/or do
// whatever you want with data
System.out.println(line) // this will show you what you // read in from file
}
bwin.close();
in.close();
}
Originally posted by Ravi Mandalapu:
Hi,
I am new to I/O's.
I want to read data from file and display that data on form. If you have any sample code....
Any help would be greatly appeciate.
Ravi

 
Ravi Mandalapu
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kim Kantola,
Thank you very much...
I fixed my problem...
ravi
Originally posted by Kim Kantola:
Hi,
the following code will read a file and return the files data a line at a time. I am not sure how you want to process your data, but this part will at least show you how to open a file and access the data a String at a time. bwin.readline() returns a null when it gets to the end of the file.
You can also use just FileReader and do in.read to return a character at a time if that better suits your purpose. (I can send another bit of code showing that if you are interested.)
Hope this helps,
Kim

public void readFile() throws IOException {
File inputFile = new File("C:\\myfile.txt");
FileReader in = new FileReader(inputFile); BufferedReader bwin = new BufferedReader(in);
String line;
while ((line=bwin.readLine()) !=null){
// code here to parse line and/or do
// whatever you want with data
System.out.println(line) // this will show you what you // read in from file
}
bwin.close();
in.close();
}

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!