Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2007

Tip: Looking for answers? Try searching our database.

Copy files in java gives me out of memory error

Thread view: 
Canned - 25 Dec 2007 15:34 GMT
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 - 25 Dec 2007 17:33 GMT
> Hi,
> I'm trying to create a class which only jobs is to copy any text files
[quoted text clipped - 21 lines]
> Can someone please tell me what I'm doing wrong? I'm not very
> experienced in Java

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.
Signature

Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Canned - 27 Dec 2007 00:47 GMT
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);
}
Stefan Ram - 27 Dec 2007 01:17 GMT
>but if I'm assigning "from.read(buffer)" to byteWrite first,
>then the apps freeze. Why can't I do this?

 Java does not have this kind of »referential transparency«,
 because not only do expressions have /values/, but their
 evaluation at run-time also has specified /effects/.

 So, even if the value of an expression was always the same,
 it might make a difference whether it is evaluated once or
 more often and when it is being evaluated.

 The evaluation of the expression »from.read(buffer)« will have
 the effect of actually reading to the buffer. The value of the
 expression »from.read(buffer)« depends on the state of the
 system on the instant of evaluation. This value will describe
 the outcome of the read operation (like how many bytes read or
 so).

 This evaluation might happen repeatedly, as it should be,
 within the while's parentheses, but only once when written
 in front of the loop.
John W. Kennedy - 27 Dec 2007 03:15 GMT
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();

Signature

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

Roedy Green - 26 Dec 2007 07:06 GMT
>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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Canned - 27 Dec 2007 00:52 GMT
Roedy Green schreef:

>> 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");
}
Roedy Green - 27 Dec 2007 23:41 GMT
On Wed, 26 Dec 2007 07:06:51 GMT, Roedy Green
<see_website@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
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 26 Dec 2007 07:11 GMT
>"main" java.lang.OutOfMemoryError: Java heap space

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

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Canned - 27 Dec 2007 00:55 GMT
Roedy Green schreef:

>> "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.
Chase Preuninger - 31 Dec 2007 01:14 GMT
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 - 31 Dec 2007 02:22 GMT
>                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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.