OutputStream - 在AppData / Roaming中拒绝访问

时间:2014-08-17 06:59:57

标签: java inputstream outputstream

我正在尝试使用输入/输出流将文件复制到C:\ Users \ user.name \ AppData \ Roaming.minecraft \ mods中,我得到的是这个错误: FileStreamsTest:java.io.FileNotFoundException:C:\ Users \ Ethan \ AppData \ Roaming.minecraft \ mods(访问被拒绝)

奇怪的是,我能够运行另一段实际生成mods文件夹并将文件下载到其中的代码。这是输入/输出流:

String username = System.getProperty("user.name");
                   File file = chooser.getSelectedFile();
                   String fullPath = file.getAbsolutePath();
                   String filename = chooser.getSelectedFile().getName();
                   String dest = ("C:\\Users\\" + username + "\\AppData\\Roaming\\.minecraft 
\\mods\\");

                   try {
                    FileInputStream fis = new FileInputStream(fullPath);
                    FileOutputStream fos = new FileOutputStream(dest);
                    int c;

                    while ((c = fis.read()) != -1) {
                       fos.write(c);
                    }

                    fis.close();
                    fos.close();
                } catch (FileNotFoundException e1) {
                    System.err.println("FileStreamsTest: " + e1);
                } catch (IOException e1) {
                    System.err.println("FileStreamsTest: " + e1);
                }

如果你想看一下,那就是我制作文件夹并将文件下载到其中的代码。

文件夹创建:

String username = System.getProperty("user.name");
        String part1 = "C:\\Users\\";
        String part2 = "\\AppData\\Roaming\\.minecraft\\mods\\";
          File theDir = new File(part1 + username + part2);

          // if the directory does not exist, create it
          if (!theDir.exists()) {
            System.out.println("creating directory: " + part1 + username + part2);
            boolean result = false;

            try{
                theDir.mkdir();
                result = true;
             } catch(SecurityException se){
                //handle it
             }        
             if(result) {    
               System.out.println("DIR created");  
             }
          }

文件下载:

tmi.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            URL website;
            try {
                website = new URL("http://marglyph.s3.amazonaws.com
/TooManyItems2014_07_15_1.7.10_Forge.jar");
                ReadableByteChannel rbc = Channels.newChannel(website.openStream());
                FileOutputStream fos = new FileOutputStream("C:\\Users\\" + username +  
"\\AppData\\Roaming\\.minecraft\\mods\\TooManyItems2014_07_15_1.7.10_Forge.jar");
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);


            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

1 个答案:

答案 0 :(得分:0)

刚刚想出这个,并认为我会分享可能遇到这个问题的人。问题是,当我进入目的地时,我没有输入文件的名称。我设置了变量,所以最后,我的输出流看起来像。

FileOutputStream fos = new FileOutputStream(dest + filename);

变量是:

String username = System.getProperty("user.name");
                   File file = chooser.getSelectedFile();
                   String fullPath = file.getAbsolutePath();
                   String filename = chooser.getSelectedFile().getName();
                   String dest = ("C:\\Users\\" + username + "\\AppData\\Roaming\\.minecraft
                   \\mods\\");