如何使用java将文件一个目录移动到另一个目录

时间:2014-05-28 12:10:30

标签: java io nio

如何使用java将文件一个目录移动到另一个目录?如果在java中有任何替代解决方案,请告诉我。

 public class FileTransform
    {
        public static void copyFile(File sourceFile, File destFile) throws IOException
        {
            if (!destFile.exists())
            {
                destFile.createNewFile();
            }
            System.out.println("Copy File Method");
            FileChannel source = null;
            FileChannel destination = null;
            try
            {
                source = new FileInputStream(sourceFile).getChannel();
                System.out.println("Destination File :"+destFile);
                destination = new FileOutputStream(destFile).getChannel();

                // previous code: destination.transferFrom(source, 0, source.size());
                // to avoid infinite loops, should be:
                long count = 0;
                long size = source.size();
                while ((count += destination.transferFrom(source, count, size - count)) < size);
            }
            finally
            {
                if (source != null)
                {
                    source.close();
                }
                if (destination != null)
                {
                    destination.close();
                }
            }
        }
        public static void main(String[] args) throws IOException
        {
             FileTransform ft = new FileTransform();
             File src= new File("C:/File/File1/A10301A0003174228I_20140528080958095/A10301A0003174228I.xml");
             File dest= new File("E:/FileDest/File1");

             ft.copyFile(src,dest);
        }
    }

上面的代码我得到的例外是

 Exception in thread "main" java.io.FileNotFoundException: E:\FileDest\File1 (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at FileTransform.copyFile(FileTransform.java:22)
    at FileTransform.main(FileTransform.java:48)

我收到文件未找到异常,请让我知道如何在java中执行此操作

3 个答案:

答案 0 :(得分:0)

                     InputStream inStream = null;
             OutputStream outStream = null;


        File afile = new File("srcfilepath");
        File bfile = new File("destfilepath");

        inStream = new FileInputStream(srcfile);
        outStream = new FileOutputStream(destfile);

        byte[] buffer = new byte[1024];

        int length;
        //copy the file content in bytes 
        while ((length = inStream.read(buffer)) > 0){

            outStream.write(buffer, 0, length);

        }

        inStream.close();
        outStream.close();

        //delete the original file
        afile.delete();

答案 1 :(得分:0)

请尝试这个。

 private boolean filemovetoanotherfolder(String sourcefolder, String destinationfolder, String filename) {
            boolean ismove = false;
            InputStream inStream = null;
            OutputStream outStream = null;

            try {

                File afile = new File(sourcefolder + filename);
                File bfile = new File(destinationfolder + filename);

                inStream = new FileInputStream(afile);
                outStream = new FileOutputStream(bfile);

                byte[] buffer = new byte[1024 * 4];

                int length;
                // copy the file content in bytes
                while ((length = inStream.read(buffer)) > 0) {

                outStream.write(buffer, 0, length);

                }


                // delete the original file
                afile.delete();
                ismove = true;
                System.out.println("File is copied successful!");

            } catch (IOException e) {
                e.printStackTrace();
            }finally{
               inStream.close();
               outStream.close();
            }
            return ismove;
            }

答案 2 :(得分:-1)

试试这个:

File yourFile= new File("C:/File/File1/A10301A0003174228I_20140528080958095/A10301A0003174228I.xml");

yourFile.renameTo(new File("E:/FileDest/File1/A10301A0003174228I.xml" ));