aspose file tools*
The moose likes Beginning Java and the fly likes How do I read an entire file all at once? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I read an entire file all at once?" Watch "How do I read an entire file all at once?" New topic
Author

How do I read an entire file all at once?

Stu Thompson
Hooplehead
Ranch Hand

Joined: Jun 14, 2006
Posts: 136
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


"This is not to say that design is unnecessary. But after a certain point, design is just speculation." --Philip Chu
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196


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

Joined: Jun 14, 2006
Posts: 136
Exactly right amount of help. Thanks!

Stu
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
You can also use a Scanner if you're using Java 1.5


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

Be advised that Read Doesn't Do What You Think It Does


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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).


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8866
    
    8

Good point. So does DataInputStream (they both implement DataInput). I'll update the FAQ.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Several Scanner methods return the Scanner itself, apparently encouraging us to do things like:

or

Wheee.
[ June 21, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ted Shaneyfelt
Greenhorn

Joined: Nov 01, 2011
Posts: 2

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

Joined: Oct 13, 2005
Posts: 38357
    
  23
Welcome to the Ranch Ted Shaneyfelt
ajay mittal
Greenhorn

Joined: Nov 23, 2011
Posts: 24
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

Joined: Aug 05, 2005
Posts: 3446
    
  12
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


Joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38357
    
  23
And welcome to the Ranch
ajay mittal
Greenhorn

Joined: Nov 23, 2011
Posts: 24
Campbell Ritchie wrote:And welcome to the Ranch



Thanks dear.
Markz Demetrez
Greenhorn

Joined: Dec 14, 2011
Posts: 1
Scanner would be a great idea.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7648
    
  19

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38357
    
  23
And welcome to the Ranch Markz Demetrez.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Mar 17, 2011
Posts: 7648
    
  19

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Nov 01, 2011
Posts: 2


Kocha Sapam
Ranch Hand

Joined: Aug 04, 2012
Posts: 35
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...
 
Consider Paul's rocket mass heater.
 
subject: How do I read an entire file all at once?