Monday, January 9, 2012

Java 7 new features - 3. NIO2 : A. Copy files with Path API


<< Previous Table of Categories Next >>


Java 7 has a new generation of NIO which is NIO2. There are some significant enhancements in NIO2. One of them is the Path API.
 
    /**
      * Feel free to copy and use this method
      * If you have any comments or better solutions, please paste on my blog: 
      * http://blog.yanmingyu.com or email me feedback.andrewyu@gmail.com 
      * 
      * When we copy files, there are several options for us now. I think copying with NIO2 
      * is the easiest one now.(Even though "copyFileWithOSCommand" is the fast one, but it may
      * not be allowed to use sometimes).
      */   
Copy files by using Windows/Liunx/Unix system command
 
    public static void copyFileWithOSCommand(String sourceFilename, String targetFilename)
        throws IOException
    {
        String cmd = "cmd /C copy ";
        String os = System.getProperty("os.name");
        os = os.toLowerCase();
        if(os.indexOf("windows")>=0)
        {
            os = "cp ";
        }
        cmd = cmd + sourceFilename + " " + targetFilename;
        Runtime.getRuntime().exec(cmd);
    }
Copy files by using traditional Stream
 
    public static void copyFileWithStream(String sourceFilename, String targetFilename)
        throws IOException
    {
        InputStream in = null;
        OutputStream out = null;
        try
        {
            in = new FileInputStream(sourceFilename);
            out = new FileOutputStream(targetFilename);
            byte[] buf = new byte[4098];
            int len;
            while ((len = in.read(buf)) > 0)
            {
                out.write(buf, 0, len);
            }
        }
        finally
        {
            if (in != null)
            {
                in.close();
            }
            if (out != null)
            {
                out.close();
            }
        }
    }
Copy files by using the first generation NIO transfer
 
    public static void copyFileWithNIO(String sourceFilename, String targetFilename)
        throws IOException
    {
        FileChannel source = null;
        FileChannel target = null;
        try
        {
            source = new FileInputStream(sourceFilename).getChannel();
            target = new FileOutputStream(targetFilename).getChannel();
            target.transferFrom(source, 0, source.size());
        }
        finally
        {
            if (source != null)
            {
                source.close();
            }
            if (target != null)
            {
                target.close();
            }
        }
    }
Copy files by using NIO2 which is provided by Java 7
 
    public static void copyFileWithNIO2(String sourceFilename, String targetFilename)
        throws IOException
    {
        Path source = Paths.get(sourceFilename);
        Path target = Paths.get(targetFilename);
        Path copy = Files.copy(source, target);
    }

No comments:

Post a Comment