Solved

How to read all the content of a file?

Posted on 2002-06-18
8
228 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

803 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