I am trying to read a text file and create a string. I am using following code:
String FILENAME = "file.txt";
File file = getApplicationContext().getFileStreamPath(FILENAME);
int size = (int) file.length();
System.out.println("size: "+size);
byte[] bytes = new byte[size];
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
System.out.println("buf string: "+new String(bytes));
size in this case is 5295164. in the last line of code, i get following exception:
FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at java.lang.AbstractStringBuilder.enlargeBuffer(AbstractStringBuilder.java:94)
at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:145)
at java.lang.StringBuilder.append(StringBuilder.java:216)
at se.egretails.ssapp.MyActivity$4.onClick(MyActivity.java:461)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4812)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
How should i solve this? Thanks in advance.
The buffer is too large. Try this:
InputStream content = new FileInputStream(file);
int numRead;
final int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
ByteArrayOutputStream outString = new ByteArrayOutputStream();
try{
while ((numRead = content.read(buffer)) != -1) {
outString.write(buffer, 0, numRead);
}
} finally {
content.close();
}
return new String(outString.toByteArray());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With