Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Copy files in java gives me out of memory error

27 views
Skip to first unread message

Canned

unread,
Dec 25, 2007, 10:34:32 AM12/25/07
to
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();
}
}

Daniel Pitts

unread,
Dec 25, 2007, 12:33:01 PM12/25/07
to
Quite simple, you're reading entire files into memory. More
specifically, you're reading the file into a StringBuilder. Don't do
that.

Use a FileInputStream, and a FileOutputStream, and copy a small amount
at a time (say 8k) using a byte array.
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Roedy Green

unread,
Dec 26, 2007, 2:06:51 AM12/26/07
to
On Tue, 25 Dec 2007 16:34:32 +0100, Canned <us...@invalid.domain>
wrote, quoted or indirectly quoted someone who said :

>Hi,
>I'm trying to create a class which only jobs is to copy any text files

You might use my FileTransfer class to copy files for you. See
http://mindprod.com/jgloss/products.html#FILETRANSFER
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green

unread,
Dec 26, 2007, 2:11:08 AM12/26/07
to
On Tue, 25 Dec 2007 16:34:32 +0100, Canned <us...@invalid.domain>
wrote, quoted or indirectly quoted someone who said :

>"main" java.lang.OutOfMemoryError: Java heap space

see http://mindprod.com/jgloss/runerrormessages.html#OUTOFMEMORYERROR

Canned

unread,
Dec 26, 2007, 7:47:40 PM12/26/07
to
Daniel Pitts schreef:

> Use a FileInputStream, and a FileOutputStream, and copy a small amount
> at a time (say 8k) using a byte array.

Thanks its working now, using a method to create backup instead of
another class. Anyway, while working with FileOutputStream I've noticed
that if Im doing this then it just run,

FileInputStream from = new FileInputStream(fromFile);
FileOutputStream to = new FileOutputStream(toFile);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesWrite;

while ((bytesWrite = from.read(buffer)) != -1) {
to.write(buffer, 0, bytesWrite);
}

but if I'm assigning "from.read(buffer)" to byteWrite first, then the
apps freeze. Why can't I do this?

FileInputStream from = new FileInputStream(fromFile);
FileOutputStream to = new FileOutputStream(toFile);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesWrite = from.read(buffer);

while (bytesWrite != -1) {
to.write(buffer, 0, bytesWrite);
}

Canned

unread,
Dec 26, 2007, 7:52:39 PM12/26/07
to
Roedy Green schreef:

> On Tue, 25 Dec 2007 16:34:32 +0100, Canned <us...@invalid.domain>
> wrote, quoted or indirectly quoted someone who said :
>
>> Hi,
>> I'm trying to create a class which only jobs is to copy any text files
>
> You might use my FileTransfer class to copy files for you. See
> http://mindprod.com/jgloss/products.html#FILETRANSFER

while (me.browsing()) {
throw new FileNotFoundException("Page doesn't exist");
}

Canned

unread,
Dec 26, 2007, 7:55:19 PM12/26/07
to
Roedy Green schreef:

> On Tue, 25 Dec 2007 16:34:32 +0100, Canned <us...@invalid.domain>
> wrote, quoted or indirectly quoted someone who said :
>
>> "main" java.lang.OutOfMemoryError: Java heap space
>
> see http://mindprod.com/jgloss/runerrormessages.html#OUTOFMEMORYERROR

Thanks but I already fix it using classes Daniel told me.

Message has been deleted

John W. Kennedy

unread,
Dec 26, 2007, 10:15:05 PM12/26/07
to
The best and fastest way to copy a file is as follows:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;

...

// Get an input channel
final FileChannel ich = new FileInputStream(input).getChannel();

// Get an output channel
final FileChannel och = new FileOutputStream(tofile).getChannel();

// Get the size of the input
final long size = ich.size();

// Begin at the beginning
long position = 0;

// Copy chunks of the file until finished
while (position < size)
position += och.transferFrom(ich, position, size - position);

// Close the files
ich.close();
och.close();

--
John W. Kennedy
"Sweet, was Christ crucified to create this chat?"
-- Charles Williams. "Judgement at Chelmsford"

Roedy Green

unread,
Dec 27, 2007, 6:41:41 PM12/27/07
to
On Wed, 26 Dec 2007 07:06:51 GMT, Roedy Green
<see_w...@mindprod.com.invalid> wrote, quoted or indirectly quoted
someone who said :

>http://mindprod.com/jgloss/products.html#FILETRANSFER
oops
http://mindprod.com/products.html#FILETRANSFER

Chase Preuninger

unread,
Dec 30, 2007, 8:14:10 PM12/30/07
to
Not sure why. The other day I worte a spell check that loads the
entire english language into memory, and I had no prolbems. The
follownig code works for me. Also it is probably faster than yours,
and works great with any type of file.

public static void makeCopy(File from, File to) throws IOException
{
if(!to.exists())
{
to.createNewFile();
}
InputStream in = new FileInputStream(from);
OutputStream out = new BufferedOutputStream(new
FileOutputStream(to));

int b;
while((b = in.read()) != -1)
{
out.write(b);
}
out.flush();
in.close();
out.close();
}

Jeff Higgins

unread,
Dec 30, 2007, 9:22:21 PM12/30/07
to

Chase Preuninger wrote:
> The other day I worte a spell check that loads the
> entire english language into memory, and I had no prolbems.

wunder wrehe i puttit.


0 new messages