简体   繁体   中英

java.io.FileNotFoundException (Access is Denied) in a static method to move file

I was met with a java.io.FileNotFoundException: C:\\Users\\520\\Desktop\\Thing (Access is denied) error when running the following script to move files. Does this mean I should run my IDE under admin privileges?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM