?
Solved

How to read all the content of a file?

Posted on 2002-06-18
8
Medium Priority
?
249 Views
Last Modified: 2010-03-31
How to read all the content of a file, so it would keep read on until it has reach the end of the file.
thanks.
0
Comment
Question by:tomboman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 7086352
try this:
         java.io.File f = new java.io.File("c:/myFile.txt");
         byte[] buffer new byte[f.length()];
         java.io.FileInputStream fis = new java.io.FileInputStream(f);
         fis.read(buffer);


the buffer will contain the file data
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7086354
try this:
         java.io.File f = new java.io.File("c:/myFile.txt");
         byte[] buffer new byte[f.length()];
         java.io.FileInputStream fis = new java.io.FileInputStream(f);
         fis.read(buffer);


the buffer will contain the file data
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7086360
... or use int read = fis.read(buffer);
read == -1; means that EOF is reached
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 92

Expert Comment

by:objects
ID: 7086774
The read() method does not guarantee to return all the requested bytes. You should do something like this:

int n;
int remaining = buffer.length;
while (remaining>0)
{
   remaining -= read(buffer, buffer.length - remaining, remaining);
}


0
 
LVL 1

Accepted Solution

by:
paragm earned 200 total points
ID: 7089282
Recomended method will be

BufferedReader br
   = new BufferedReader(new FileReader("foo.in"));
String line=null;
           while ( (line = br.readLine()) != null)
                System.out.println(line);    
            }
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7092282
why do you thing that this is the recomended method?!?
you can use BufferedReader only if you have a text file !!!
0
 

Author Comment

by:tomboman
ID: 7095574
thanks. paragm, short and simple. and others, thanks too.
0
 
LVL 92

Expert Comment

by:objects
ID: 7097827
If you'd stated in your question that you only wanted to read text files you would have saved us all a bit of time.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question