This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FILE I/O

 
Gaurav x Jain
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

output is:- howdy
why the other one(ydowh) not in output. Please help me
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags for the source code posted.

Also you need to loop through the entire contents of the file and print them. In your code- You are just getting the first line- readLine()- would read one line. But your file contains 2 lines.
 
Gaurav x Jain
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you but I try for loop but it still prints howdy. please find the code as below:

/*Reading Stuff*/
FileReader fr=new FileReader(fileObj);
BufferedReader br=new BufferedReader(fr);
String Data=br.readLine();
for(int i=0;i<fileObj.length();i++)
{
System.out.println(Data);
}


Please guide me

>
 
Abhijeet Sutar
Greenhorn
Posts: 17
Android Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For reading the file line by line ,you have to use BufferedReader.readLine() method which returns next line text ,if it end of the file (Stream) it returns null
Following is the code for reading data from file



 
Campbell Ritchie
Sheriff
Posts: 48409
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The File#length() method doesn't tell you how many lines you have in your file.
 
Gaurav x Jain
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your help!!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic