Java file IO question

So

Lifer
Jul 2, 2001
25,923
17
81
Weird, I know. It's very troubling to me that arrays are immutable (although I understand why it's banned :cool: ).

Basically, I have an arbitrary binary file, which I wish to read in, one byte at a time, manipulate the data, and write it out.

So, I need a means to extract a file length to determine what size to make the array, *and* for error checking (I need to know if file A is bigger than file B).

Any simple means to extract my file length?
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
Reading entire file directly into array is generally a pretty bad idea, unless you know for a fact that the file is rather small.

That being said, there are lots of ways of doing what you need. You can use something out of java.util. package (ArrayList, etc). You can read file in chunks into a smaller array buffer and process it one file at a time.
 

So

Lifer
Jul 2, 2001
25,923
17
81
Originally posted by: Argo
Reading entire file directly into array is generally a pretty bad idea, unless you know for a fact that the file is rather small.

That being said, there are lots of ways of doing what you need. You can use something out of java.util. package (ArrayList, etc). You can read file in chunks into a smaller array buffer and process it one file at a time.

Hmm, good point. I don't really need to read the entire file into memory, so long as I know the length of the file beforehand.

I'll have to take a look at the java.util package. Thanks!
 

flashbacck

Golden Member
Aug 3, 2001
1,921
0
76
You'll want to use BufferedInputStream, no? Otherwise your harddrive will be accessing everytime you request the next byte.

Also, File.length should give you the file length in bytes.

 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
Guys slow down.... don't bother with memory mapped IO and stuff.

It looks like you're a beginner, so stick to the basics first.

THere are many ways to figure out the file size, the easiest is to create a java.lang.File object to your file, for example: File f = new File("c:\\foo.txt"), then do f.length() to get the file size.

You can use the same File object to construct your FileInputStream. Once you have a input stream you can read the whole file contents into a byte[] array using the 3-argument read method... So the whole thing will look something like this:

File f = new File("c:\\foo.txt");
FileInputStream in = new FileInputStream(f);
int length = (int)f.length();
byte[] data = new byte[length];
in.read(data, 0, length);
// the contents of the file should be read into the data array.


http://java.sun.com/j2se/1.4.2...o/FileInputStream.html
http://java.sun.com/j2se/1.4.2.../api/java/io/File.html

Now, this is a bad way to read files that are more than a several k (say 32k) large. You'll need to be more creative with larger files.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
Originally posted by: flashbacck
You'll want to use BufferedInputStream, no? Otherwise your harddrive will be accessing everytime you request the next byte.
I doubt there's any modern operating system or hard drive that will read a byte at a time. Of course, it's still a good idea to use a buffered stream so that there is also a user-space buffer.
 

exdeath

Lifer
Jan 29, 2004
13,679
10
81
Quickly in pseudo code:

open input file;
open output file;
while(input file has records)
{
read record;
process record;
write record to output;
}
close output;
close input;

Don't worry about performance, even reading one byte at a time, at this point. Any operating system worth anything will be caching and prefetching data in large blocks behind the scenes. Only when you become CPU limited will the overhead of the file IO API calls start to add up if you have large files with lots of small records, but the OS will be making use of DMA and prefetching to ensure that the data is being read from memory buffers and not the disk.