Não é mais possível fazer postagens ou usar assinaturas novas da Usenet nos Grupos do Google. O conteúdo histórico continua disponível.
Dismiss

Copy files in java gives me out of memory error

27 visualizações
Pular para a primeira mensagem não lida

Canned

não lida,
25 de dez. de 2007, 10:34:3225/12/2007
para
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

não lida,
25 de dez. de 2007, 12:33:0125/12/2007
para
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

não lida,
26 de dez. de 2007, 02:06:5126/12/2007
para
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

não lida,
26 de dez. de 2007, 02:11:0826/12/2007
para
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

não lida,
26 de dez. de 2007, 19:47:4026/12/2007
para
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

não lida,
26 de dez. de 2007, 19:52:3926/12/2007
para
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

não lida,
26 de dez. de 2007, 19:55:1926/12/2007
para
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.

A mensagem foi excluída

John W. Kennedy

não lida,
26 de dez. de 2007, 22:15:0526/12/2007
para
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

não lida,
27 de dez. de 2007, 18:41:4127/12/2007
para
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

não lida,
30 de dez. de 2007, 20:14:1030/12/2007
para
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

não lida,
30 de dez. de 2007, 21:22:2130/12/2007
para

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 nova mensagem