Results 1 to 4 of 4
  1. #1
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default Problem while writing to file, size greater than 15MB ..?

    Dear Members,

    FileReader reader = new FileReader("c:/sample.sql");
    BufferedReader buffer = new BufferedReader(reader);
    String readLine = null;
    StringBuffer line = new StringBuffer();
    while ((readLine = buffer.readLine()) != null)
    {
    line.append(readLine + "\n");
    }

    In the above code "sample.sql" file size is around 15MB so it throws an "java.outof Memory exception" Java Heap space error...

    Please help me how to write a large file to a file using java.

    Also i need the full data from the file, i need all the data of size 15MB to write to a file.


    Regards,
    Prabhu.

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    Sounds like you want to read the contents of one file and then write it to another file. If that is the case, don't read the entire file into memory and then write it back out. Read a block (line or whatever) and then write it. Get rid of the StringBuffer.

  3. #3
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default Problem while writing to file, using string buffer

    @Steve

    Hi steve it sounds great, however is it ok to do by setting the StringBuffer length, by using StringBuffer.setLength(File.length());

    Please let me know if it is bad idea.

    How to read a file upto a certain block and write to a file and then the remaining.
    Please do you have any sample code for this it would be better.

    Also my file size would be greater than 1GB or 2GB.

    Regards,
    Prabhu.

  4. #4
    prabhurangan is offline Member
    Join Date
    May 2008
    Posts
    69
    Rep Power
    0

    Default

    Hi Steve,

    Sorry my idea will not be better as im getting the data as an InputStream, also i would not be knowing the file size at the beginning.

    Also i forgot to let you know:

    Im getting the input data to file as an InputStream, then by using bufferedReader im reading it and appending it to a stringbuffer.

Similar Threads

  1. Problem while writing xml file
    By Rajesh Sahu in forum Java Servlet
    Replies: 0
    Last Post: 12-23-2008, 04:53 PM
  2. Replies: 7
    Last Post: 10-28-2008, 01:41 PM
  3. Replies: 9
    Last Post: 07-10-2008, 04:52 PM
  4. Replies: 1
    Last Post: 06-30-2008, 07:14 AM
  5. File size
    By eva in forum New To Java
    Replies: 2
    Last Post: 12-19-2007, 09:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •