Solved

How to read all the content of a file?

Posted on 2002-06-18
8
233 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
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 50 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simple java question 3 59
why cannot we forward request once the response is committed 2 46
javap bin 2 40
Fast way to search item into Java Array (Rhino compatible) 2 31
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

856 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