如何在Java中将文件从一个目录移动到另一个目录

问题描述 投票:0回答:3

如何使用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中执行此操作

java io nio
3个回答
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();

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

-1
投票

尝试一下:

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

yourFile.renameTo(new File("E:/FileDest/File1/A10301A0003174228I.xml" ));
© www.soinside.com 2019 - 2024. All rights reserved.