Copy files in java gives me out of memory error
Hi,
I'm trying to create a class which only jobs is to copy any text files
before its get opened by text editor. I build a constructor that will
read from file then store its content in textContent variable. From
there, copy method would read from textContent and write its content in
a new file. But instead of doing its job, I get "Exception in thread
"main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2882)
at
java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:390)
at java.lang.StringBuilder.append(StringBuilder.java:119)
at v1.CreateBackup.<init>(CreateBackup.java:16)
at v1.EditorSWT.openFileDialog(EditorSWT.java:150)
at v1.EditorSWT.access$0(EditorSWT.java:141)
at v1.EditorSWT$1.widgetSelected(EditorSWT.java:107)
at org.eclipse.swt.widgets.TypedListener.handleEvent(Unknown Source)
at org.eclipse.swt.widgets.EventTable.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Widget.sendEvent(Unknown Source)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Unknown Source)
at org.eclipse.swt.widgets.Display.readAndDispatch(Unknown Source)
at v1.EditorSWT.main(EditorSWT.java:34)
"
Can someone please tell me what I'm doing wrong? I'm not very
experienced in Java
**********************code**************************
package v1;
import java.io.*;
public class CreateBackup {
private String textContent;
public CreateBackup(String fromFile) {
try {
File input = new File(fromFile);
BufferedReader br = new BufferedReader(new FileReader(input));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line + "\n");
}
br.close();
textContent = sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void copy(String toFile) {
try {
File output = new File(toFile);
PrintWriter out = new PrintWriter(new BufferedWriter(new
FileWriter(output)));
out.println(textContent);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
*******this is how I call CreateBackup in main class*******************
private void openFileDialog() {
FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setText(getResourceString("dialog.File.Open.text"));
dialog.setFilterPath(System.getProperty("user.dir"));
String[] filterExt = {"*.txt", "*.rtf", "*.doc", "*.*"};
dialog.setFilterExtensions(filterExt);
String fileName = dialog.open();
// TODO Create backup file first before open the file
String outFile = fileName + extension;
CreateBackup backup = new CreateBackup(fileName);
backup.copy(outFile);
// Open the file
try {
File selectedFile = new File(fileName);
if (selectedFile.isFile()) {
try {
BufferedReader br = new BufferedReader(new FileReader(selectedFile));
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
text.setText(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (NullPointerException e) {
e.printStackTrace();
}
}