Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I read an entire file all at once?

 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly right amount of help. Thanks!

Stu
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also use a Scanner if you're using Java 1.5
 
Joe Ess
Bartender
Posts: 9320
10
Linux Mac OS X Windows
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Joe Ess
Bartender
Posts: 9320
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. So does DataInputStream (they both implement DataInput). I'll update the FAQ.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Several Scanner methods return the Scanner itself, apparently encouraging us to do things like:

or

Wheee.
[ June 21, 2006: Message edited by: Stan James ]
 
Ted Shaneyfelt
Greenhorn
Posts: 2
Linux Mac PPC Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Ted Shaneyfelt
 
ajay mittal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
ajay mittal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:And welcome to the Ranch



Thanks dear.
 
Markz Demetrez
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scanner would be a great idea.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch Markz Demetrez.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ted Shaneyfelt
Greenhorn
Posts: 2
Linux Mac PPC Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Kocha Sapam
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic