java.io.FileNotFoundException(Access is Denied)在静态方法中移动文件

时间:2013-10-24 15:57:46

标签: java io

运行以下脚本移动文件时遇到java.io.FileNotFoundException: C:\Users\520\Desktop\Thing (Access is denied)错误。这是否意味着我应该在管理员权限下运行我的IDE?

public static void moveFiles(){
        InputStream inStream = null;
        OutputStream outStream = null;
        try{
            File afile = new File("C:\\Users\\520\\Desktop\\hey.txt"); // Gotta specify initial path. Consider adding an input for this
            File bfile = new File("C:\\Users\\520\\Desktop\\Thing");

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

            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();

            afile.delete();
            System.out.println("File was copied successfully!");
        }catch(IOException e){
            e.printStackTrace();
        }
    }

1 个答案:

答案 0 :(得分:0)

使用它来调整目标文件对象,这样如果它是一个目录,它将使用该目录中的源文件名。

if (bfile.isDirectory()) bfile = new File(bfile, afile.getName());