Forums Register Login

How do I read an entire file all at once?

+Pie Number of slices to send: Send
Hi,

I want to read an entire file in all at once. It always be a text file no more than a few hundred bytes. The processing is very limited and it seems like overkill to have some sort of loop reading in the file...

How do I just place the entire file's contents into a String in one statement? It must be possible, yes?

tia,

Stu
+Pie Number of slices to send: Send


file, of course, is the variable refering to a File object representing the file.
fis, of course, is the FileInputStream opened using this File object.

Refer to the File and FileInputStream API doc for more info.
[ June 21, 2006: Message edited by: Martin Simons ]
+Pie Number of slices to send: Send
Exactly right amount of help. Thanks!

Stu
+Pie Number of slices to send: Send
You can also use a Scanner if you're using Java 1.5
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
You could use RandomAccessFile instead of FileInputStream. There is a readFully() method there, which does guarantee to fill the array (assuming the file has enough bytes).
+Pie Number of slices to send: Send
Good point. So does DataInputStream (they both implement DataInput). I'll update the FAQ.
+Pie Number of slices to send: Send
Several Scanner methods return the Scanner itself, apparently encouraging us to do things like:

or

Wheee.
[ June 21, 2006: Message edited by: Stan James ]
+Pie Number of slices to send: Send
System.out.println(new java.util.Scanner(System.in).findWithinHorizon("(s).*",0));

Use a File in place of System.in (but please note that using Scanner has a limitation that nothing following the last newline is used, so make sure your file contains a newline at the end if you intend to use this method).

The dot matches every character except possibly a newline. The (s) removes that exception. The asterisk makes it match as many such characters as there are, (zero or more characters).

Not reading the line with the EOF mark on it is probably an API bug, unless it's because of some obscure rule in the specification.
+Pie Number of slices to send: Send
Welcome to the Ranch Ted Shaneyfelt
+Pie Number of slices to send: Send
Hi guys,

I am new to javaranch.

Reading the contents of the entire file all at once can be achieved through -

Path file = ...;
byte[] fileArray;
fileArray = Files.readAllBytes(file);


note :- but the file should be small in size.

Ajay Mittal
+Pie Number of slices to send: Send
 

ajay mittal wrote:Reading the contents of the entire file all at once can be achieved through -

Path file = ...;
byte[] fileArray;
fileArray = Files.readAllBytes(file);


Note that this will only work in Java 7
+Pie Number of slices to send: Send
And welcome to the Ranch
+Pie Number of slices to send: Send
 

Campbell Ritchie wrote:And welcome to the Ranch




Thanks dear.
+Pie Number of slices to send: Send
Scanner would be a great idea.
+Pie Number of slices to send: Send
 

Markz Demetrez wrote:Scanner would be a great idea.


It's worth remembering that Scanner will probably (don't know about v7) be a lot slower than a BufferedReader or BufferedInputStream, because it was designed to scan text, not read files. Personally, I don't even think it does a great job of that, but I am admittedly biased.

Winston
+Pie Number of slices to send: Send
And welcome to the Ranch Markz Demetrez.
+Pie Number of slices to send: Send
 

Stu Thompson wrote:it seems like overkill to have some sort of loop reading in the file...



It's not. Not at all. Why do you think it is?

How do I just place the entire file's contents into a String in one statement? It must be possible, yes?



Why would you assume that? I mean, other than the fact that anything can be done in one statement just by putting it into a method.

And yes, I see that in Java 7, the core API now does in fact have a means to do that. However, it puts it into a byte array, which may not be what you want, given that it's a text file. And note that a) Internally that method must be looping, and b) Whatever it does is no different than something you could have written yourself and stuck into a utility method that you keep around in case you need this functionality again.
+Pie Number of slices to send: Send
 

Jeff Verdegan wrote:

Stu Thompson wrote:it seems like overkill to have some sort of loop reading in the file...

It's not. Not at all. Why do you think it is?


Seems to be the week for reviving old threads. Don't worry, I was guilty the other day.

Winston
+Pie Number of slices to send: Send
 

Winston Gutkowski wrote:

Jeff Verdegan wrote:

Stu Thompson wrote:it seems like overkill to have some sort of loop reading in the file...

It's not. Not at all. Why do you think it is?


Seems to be the week for reviving old threads. Don't worry, I was guilty the other day.

Winston



I almost never remember to pay attention to the dates.
+Pie Number of slices to send: Send

+Pie Number of slices to send: Send
This is not an overkill :

public String notOverKill (String arg) {
File f = new File(arg);
byte b[] = new byte[(int) f.length()];
String str[] = new String[(int) f.length()];
try {
FileInputStream fis = new FileInputStream(f);
fis.read(b);

for (int i = 0; i < b.length; i++) str[i] = (char) b[i];
return new String(str, 0, str.length-1);
}
catch (Exception e) {
return null;
}
}

that's my two cents

N.B. - I love the code that's a lil' bit longer than the shorter alternative

Edit : Damn, that was cruel, I forgot to see the date
My apology to everyone...
Whatever you say buddy! And I believe this tiny ad too:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 70309 times.
Similar Threads
6GB data as String
Student schedule
OutOfMemory
replacing content in a file
Swing Open Dialog and the rest of my program
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:28:01.