• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I read an entire file all at once?

 
Hooplehead
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 136
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly right amount of help. Thanks!

Stu
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also use a Scanner if you're using Java 1.5
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Be advised that Read Doesn't Do What You Think It Does
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point. So does DataInputStream (they both implement DataInput). I'll update the FAQ.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 2
Mac PPC Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Ted Shaneyfelt
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
ajay mittal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:And welcome to the Ranch




Thanks dear.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scanner would be a great idea.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch Markz Demetrez.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac PPC Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
reply
    Bookmark Topic Watch Topic
  • New Topic