Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I process large binary files(100MB+)?

Status
Not open for further replies.

no137

Programmer
Feb 5, 2002
7
US
How can I process large binary files(100MB+)? I have this code from javaworld site. I want to process large binary files without running out of memory. I want to move large files as a java objects from one directory to another. But with this code, I am getting memory errors on very large files. Please Help.

Thanks
Fred


import java.io.*;

public class Utils {

public Utils() {
}

public static byte[] getRawData(String filePath) {
byte[] data = null;

File file = new File(filePath);
if (!file.exists()) {
return null;
}

int contentLength = (int)file.length();
if (contentLength == 0) { // file not found
return null;
}

try {
data = new byte[(int)contentLength];
FileInputStream fis = new FileInputStream(file);
fis.read(data);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}


public static void putRawData(byte[] data, String filePath) {
try {
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(data);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
 
You're creating a byte array in memory the size of the entire file. No wonder you're getting memory exceptions!

Try allocating a modest byte array and using the read( byte[], offset, length) and write( byte[], offset, length) methods to move the file in chunks. You'll need to keep track of the offset in a variable. The "length" is the size of the byte array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top