Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to read all the content of a file?

Posted on 2002-06-18
8
Medium Priority
?
254 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

618 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