Solved

How to read all the content of a file?

Posted on 2002-06-18
8
208 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
Comment Utility
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
Comment Utility
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
Comment Utility
... or use int read = fis.read(buffer);
read == -1; means that EOF is reached
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Accepted Solution

by:
paragm earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
thanks. paragm, short and simple. and others, thanks too.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
endX challenge 2 47
countPairs challenge 7 57
Starting to learn JAVA, 7 46
Java array passed to SQL where clause 23 38
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now