Jump to content

Java: Copy 50% of file


Deveant
 Share

Recommended Posts

Im currently just playing around in Java, and am getting annoyed :( what im trying to do, is copy only 50% of the bytes from Src to DstA, then copy the other 50% to DstB. Then concatenate DstA and DstB to recreate the original file.

So far no luck... What ive got so far can copy file A -> B, but wont allow me to read Src from given position.

Any help would be much appreciated.

for my understanding i should be able to be able to give the starting position within the line:

MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);

but when ever i dont use 0, i get Caught IOException: Access is denied.

import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;

public class split {

    /** Fast & simple file copy. */
    public static void copy(File source, File dest) throws IOException {
         FileChannel in = null, out = null;
         try {          
              in = new FileInputStream(source).getChannel();
              out = new FileOutputStream(dest).getChannel();
     
              long size = in.size();
              System.out.println(size);
              MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
     
              out.write(buf);
     
         } finally {
              if (in != null)          in.close();
              if (out != null)     out.close();
         }
    }


    
    public static void main(String[] args) 
    {
        try 
        {
            copy(new File("C:\\hello.txt"), new File("C:\\hel.txt"));
        } 
        catch (IOException e) {
            System.err.println("Caught IOException: " + e.getMessage());
        }
    }
}

Link to comment
Share on other sites

Im currently just playing around in Java, and am getting annoyed :( what im trying to do, is copy only 50% of the bytes from Src to DstA, then copy the other 50% to DstB. Then concatenate DstA and DstB to recreate the original file.

So far no luck... What ive got so far can copy file A -> B, but wont allow me to read Src from given position.

Any help would be much appreciated.

for my understanding i should be able to be able to give the starting position within the line:

MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);

but when ever i dont use 0, i get Caught IOException: Access is denied.

import java.io.*;
import java.nio.*;
import java.nio.channels.FileChannel;

public class split {

    /** Fast & simple file copy. */
    public static void copy(File source, File dest) throws IOException {
         FileChannel in = null, out = null;
         try {          
              in = new FileInputStream(source).getChannel();
              out = new FileOutputStream(dest).getChannel();
     
              long size = in.size();
              System.out.println(size);
              MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
     
              out.write(buf);
     
         } finally {
              if (in != null)          in.close();
              if (out != null)     out.close();
         }
    }


    
    public static void main(String[] args) 
    {
        try 
        {
            copy(new File("C:\\hello.txt"), new File("C:\\hel.txt"));
        } 
        catch (IOException e) {
            System.err.println("Caught IOException: " + e.getMessage());
        }
    }
}

Below is a copy of my solution.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileToByteArray
{
    private File file;

    public FileToByteArray(File file)
    {
        this.file = file;
    }

    private byte[] firstHalf;
    private byte[] secondHalf;

    public byte[] getBytesFromFile() throws IOException
    {
        InputStream is = new FileInputStream(file);

        long length = file.length();

        if (length > Integer.MAX_VALUE)
        {
            // the file is to large
            return null;
        }

        // Create the byte array to hold the data
        byte[] bytes = new byte[(int) length];

        firstHalf = new byte[(int) length / 2];
        secondHalf = new byte[(int) length - firstHalf.length];

        int offset = 0;
        int numRead = 0;
        while (offset < bytes.length
                && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0)
        {
            offset += numRead;
        }

        if (offset < bytes.length)
        {
            throw new IOException("Could not completely read file "
                    + file.getName());
        }

        is.close();
        return bytes;
    }

    public void splitByteArray(byte[] array)
    {
        for (int i = 0; i < array.length; i++)
        {
            if (i < firstHalf.length)
                firstHalf[i] = array[i];
            else
                secondHalf[i - firstHalf.length] = array[i];
        }

    }

    public void printTwoHalves()
    {
        System.out.println("////////FIRST HALF/////////");
        for (int i = 0; i < firstHalf.length; i++)
        {
            System.out.print((char) firstHalf[i]);
        }
        System.out.println();
        System.out.println("////////SECOND HALF/////////");
        for (int i = 0; i < firstHalf.length; i++)
        {
            System.out.print((char) secondHalf[i]);
        }
    }

    public void mergeTwoHalves(byte[] merged)
    {
        System.arraycopy(firstHalf, 0, merged, 0, firstHalf.length);
        System.arraycopy(secondHalf, 0, merged, firstHalf.length,
                firstHalf.length);

        System.out.println();
        System.out.println("////////MERGED/////////");
        for (int i = 0; i < merged.length; i++)
        {
            System.out.print((char) merged[i]);
        }
    }

    public static void main(String[] args)
    {
        File f = new File("C:/Hello.txt");
        FileToByteArray program = new FileToByteArray(f);

        try
        {
            byte[] fileAsByteArray = program.getBytesFromFile();
            if (fileAsByteArray == null)
                return;

            program.splitByteArray(fileAsByteArray);
            program.printTwoHalves();
            byte[] bytes = new byte[fileAsByteArray.length];
            program.mergeTwoHalves(bytes);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

Link to comment
Share on other sites

FWIW, the original code snippet works for me.

If I set the starting position to something other than 0, I do need to reduce the size by an equal amount for obvious reasons. Failing to do so will result in an 'Invalid argument' IOException.

Similarly, if I set position beyond size, the program fails with an 'Invalid argument' IOException.

So, not really sure why you get an Access Denied, but I suspect you need to take a closer look at the files you're copying themselves.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...