Reputation: 9013
I'm a noob to Java so may be missing something obvious but I have a function which works for files files in the 200-300k range without issue but once I get to 1.4mb it falls over silently!
Here's the code:
private String readOutputFile(String filename) {
if (filename == null) {
return null;
}
File file = new File(filename);
FileInputStream fis = null;
String fileContent = "";
this.logger.log("Reading " + filename + " from filesystem.");
try {
fis = new FileInputStream(file);
System.out.println("Total file size to read (in bytes) : " + fis.available());
int content;
while ((content = fis.read()) != -1) {
fileContent += (char) content;
}
} catch (IOException e) {
this.logger.log("IO Problem reading ITMS output file\n");
e.printStackTrace();
throw new Error("io-error/itms-output");
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
this.logger.log("IO Problem reading and/or closing ITMS output file\n");
ex.printStackTrace();
throw new Error("io-error/finally-block");
}
}
this.logger.log("File content has been read in");
String compressed = this.compress(this.cleanXML(fileContent));
this.logger.log("The compressed file size is :" + compressed.length() + " bytes");
return compressed;
}
When it hits the size threshold which creates it to fail, it seems to stay within the while loop or at least that's my assumption because while it does report to the console the "Total file size to read ..." it never reaches the "File content has been read in" logging.
Upvotes: 0
Views: 120
Reputation: 201497
You are creating many temporary String
objects by performing character concatenation in your loop. I would use a StringBuilder
. I would also prefer a try-with-resources
. And if at all possible, I would prefer to stream from the InputStream
to the OutputStream
directly (instead of reading this entirely into memory). Anyway, based on what is here,
private String readOutputFile(String filename) {
if (filename == null) {
return null;
}
File file = new File(filename);
StringBuilder sb = new StringBuilder();
this.logger.log("Reading " + filename + " from filesystem.");
try (FileInputStream fis = new FileInputStream(file)) {
System.out.println("Total file size to read (in bytes) : " + fis.available());
int content;
while ((content = fis.read()) != -1) {
sb.append((char) content);
}
} catch (IOException e) {
this.logger.log("IO Problem reading ITMS output file\n");
e.printStackTrace();
throw new Error("io-error/itms-output");
}
this.logger.log("File content has been read in");
String compressed = this.compress(this.cleanXML(sb.toString()));
this.logger.log("The compressed file size is : " + compressed.length() + " bytes");
return compressed;
}
Upvotes: 1