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();
}
}
}
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();
}
}
}