簡體   English   中英

靜態方法中的java.io.FileNotFoundException(訪問被拒絕)來移動文件

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

運行以下腳本移動文件時,遇到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();
        }
    }

使用它來調整目標文件對象,以便如果它是一個目錄,它將使用該目錄中的源文件名。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM