Results 1 to 5 of 5
  1. #1
    me.anchit is offline Member
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Post File Handling: Problem in copy paste.

    I used a program in java to copy paste a file.
    The contents of the file are read in byte array in chunks of (8*1024) bytes, but the contents are not copied properly.
    The MD5 checksum comes out to be different.
    Please help.

    public void copy(File source, File dest)
    {
    int reader=0, buffersize=8*1024;
    byte[] buffer=new byte[buffersize];
    try
    {
    if(!source.isFile())
    {
    System.out.println("Not a file");
    }
    else
    {
    FileOutputStream fos=new FileOutputStream(dest+"/"+source.getName());
    FileInputStream fis=new FileInputStream(source);
    while((reader=fis.read(buffer))!=-1)
    {
    if(fis.available()<(8*1024) && fis.available()>0)
    {
    buffersize=fis.available();
    buffer=new byte[buffersize];
    }
    fos.write(buffer);
    fos.flush();
    }
    fos.close();
    fis.close();
    }
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: File Handling: Problem in copy paste.

    Please use [code] tags [/code] when posting code, otherwise it's almost unreadable.

    When you read into a buffer there is no guarantee that the buffer will be filled with data.
    The read method returns the number of bytes read.
    You need to use that number when writing to tell the output stream how many of the bytes in the buffer to write.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    me.anchit is offline Member
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default Re: File Handling: Problem in copy paste.

    Ok, I'll remember to use code tags.
    I did not get you. Please give an example.
    Thanks.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,014
    Rep Power
    20

    Default Re: File Handling: Problem in copy paste.

    The read() method returns a number.
    That number is the number of bytes read, into the buffer.
    It does not read 8k everytime.
    You need to use that number in the write() method.

    Look at the API.
    InputStream.read()
    OutputStream.write()
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    me.anchit is offline Member
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default Re: File Handling: Problem in copy paste.

    Thank You very much.
    My problem is solved. :)

Similar Threads

  1. cut/copy/paste in JTextField
    By gauravmanral in forum AWT / Swing
    Replies: 14
    Last Post: 09-25-2011, 07:07 AM
  2. JTable copy and paste problem on Mac
    By willlotr in forum AWT / Swing
    Replies: 4
    Last Post: 04-07-2011, 07:15 PM
  3. Copy, Cut and Paste Problems!
    By bones in forum New To Java
    Replies: 4
    Last Post: 07-17-2009, 10:18 AM
  4. Cut, copy and Paste in JTextPane
    By Gudradain in forum AWT / Swing
    Replies: 1
    Last Post: 01-03-2009, 06:43 AM
  5. Problems with copy paste
    By fredand44 in forum Eclipse
    Replies: 0
    Last Post: 12-17-2008, 04:14 PM

Tags for this Thread

Posting Permissions

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