如何将文件从文件夹移动到文件夹

时间:2018-09-02 08:02:54

标签: java file access denied

Iam将文件下载到我的PC中,然后将其移入

“ C:/程序文件(x86)/MyProgram/sample.txt”

问题是iam尝试将文件从下载文件移到目标文件

这是我的代码

String website ="https://www.sample-videos.com/text/Sample-text-file-100kb.txt";
       try (InputStream in = URI.create(website).toURL().openStream()) {
         long x=Files.copy(in, Paths.get("C:/Users/Big Foot/Downloads/sample1.txt"));
         System.out.println(x); 
         in.close();
         File dest = new File("C:/Program Files (x86)/myProgram/sample.txt");
         File source = new File("C:/Users/Big Foot/Downloads/sample1.txt");
         try {
             FileUtils.moveFile(source, dest);
         } catch (IOException e) {
             e.printStackTrace();
         }
       }

对于重复索偿的人,我尝试了此解决方案Solution-1 Solution-2 Solution-3,但没有任何方法可以解决问题

这个例外出现在我身上

java.io.FileNotFoundException: C:\Program Files (x86)\Excell To Access Convertor\sample.txt (Access is denied)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at org.apache.commons.io.FileUtils.doCopyFile(FileUtils.java:1123)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1076)
at org.apache.commons.io.FileUtils.copyFile(FileUtils.java:1028)
at org.apache.commons.io.FileUtils.moveFile(FileUtils.java:3008)
at download.main(download.java:31)

1 个答案:

答案 0 :(得分:2)

没有管理员权限,您无法在C:\Program Files (x86)\中写入或修改文件。以管理员身份运行程序是解决此问题的最简单方法。