【问题标题】:How to move file one directory to another directory using in java如何在java中使用将文件一个目录移动到另一个目录
【发布时间】:2014-05-28 12:10:30
【问题描述】:

如何在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)

我收到 File not found 异常,请告诉我如何在 java 中执行此操作

【问题讨论】:

  • 嗯,错误信息很清楚为什么会抛出异常。
  • 异常显示“访问被拒绝”。你对“E:\FileDest\”有写权限吗?
  • 您似乎没有权限将文件写入E:\驱动器或E:\不存在。

标签: java io nio


【解决方案1】:
                     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();

【讨论】:

    【解决方案2】:

    请试试这个。

     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;
                }
    

    【讨论】:

      【解决方案3】:

      试试这个:

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

      【讨论】:

      • 不会移动文件吗?
      • 它会移动,但错误与目标文件夹中的访问权限有关。
      • 我确实看到了错误,但这个解决方案是针对声明的 - '请让我知道是否有任何替代解决方案可以在 java 中执行此操作。'
      • 您的解决方案因此无法正常工作:FileNotFoundException: E:\FileDest\File1 (Access is denied)
      • @vbera 我有权在 E 驱动器中写入文件,为什么会出现错误(访问被拒绝)
      猜你喜欢
      • 2014-04-16
      • 2019-09-26
      • 2017-07-20
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多