2016-08-18 11 views
0

ので、私はこのエラーを取得しています:java.io.FileNotFoundException:(アクセスが拒否されました)私は、ファイルへのアクセス権を持っている場合

java.io.FileNotFoundException:C: \ドキュメントを検閲\ \ Users \ユーザーElectrocodeプロダクション\テンプレート\リソース\画像(アクセスが拒否されました)

私はこのコードを実行すると:私は、私は別のように、ファイルへの書き込みアクセス権を持っているかなり確信しているという事実にもかかわらず

package com.template; 

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

public class Copy { 
    static File folder = new File(System.getProperty("user.home") + "/Documents/Electrocode Productions/template"); 

    public static void writeSaves() { 
     File imagesFile = new File(folder + "/resources/images"); 
     if(!imagesFile.exists()) { 
      try { 
       InputStream input = (Main.class.getResourceAsStream("/resources/images")); 
       BufferedInputStream buffedInput = new BufferedInputStream(input); 
       File fileFolder = new File(folder + "/resources/images"); 
        try { 
         fileFolder.mkdirs(); 
        } catch(SecurityException e) { 
         Log.error(e); 
        } 
       OutputStream output = new FileOutputStream(fileFolder); 
       BufferedOutputStream buffedOutput = new BufferedOutputStream(output); 
       byte[] buffer = new byte[input.available()]; 
       System.out.println(buffer); 
       int bytesRead; 
       while((bytesRead = buffedInput.read(buffer)) > 0) { 
        buffedOutput.write(buffer, 0, bytesRead); 

       } 
       input.close(); 
       output.close(); 
      } catch(IOException e) { 
       Log.error(e); 
      } 
     } 

を私のコードのセクション:

 public static void dump() { 
     if(!folder.exists()) { 
      try { 
       folder.mkdirs(); 
      } catch(SecurityException e) { 
       Log.error(e); 
      } 
     } 

     Path oldFilePath = Paths.get(folder + "/latest.log"); 
     Path newFilePath = Paths.get(folder + "/" + time.getDayOfMonth() + "." + time.getMonth() + "." + time.getYear() + "_" + time.getHour() + "." + time.getMinute() + "." + time.getSecond() + ".log"); 


     if(Config.get("keeplogs").equals("true")) { 
      try(BufferedWriter writer = Files.newBufferedWriter(newFilePath, StandardOpenOption.CREATE, StandardOpenOption.APPEND, StandardOpenOption.WRITE)) { 
       @SuppressWarnings("resource") 
       Scanner scanner = new Scanner(oldFilePath); 
       while(scanner.hasNextLine()) { 
        writer.write(scanner.nextLine()); 
        writer.newLine(); 
       } 
      } catch(IOException e) { 
       e.printStackTrace(); 
      } 
     } else if(Config.get("keeplogs").equals("false")) { 

     } else { 
      Log.warning("Unknown argument " + Config.get("log") + " in config.properties. Must be true or false"); 
     } 
    } 

私は間違っていますか?これは重複しているかもしれませんが、私がここで見つけることができるすべてのもの(StackOverflow)を見て、それを試したものではないかもしれません。何も働いていないようです。

+0

'catch'ブロックが' java.io.FileNotFoundException'をキャッチしていますか?最初のコードブロックで例外がスローされた場合、2番目のコードブロックが実行されると、どのようにファイルへの書き込みアクセス権があることがわかりますか? –

+0

} catch(IOException e){ Log.error(e); } は例外をキャッチし、2番目のコードブロックが実際に正常に実行されます。 編集:私は誤ってshiftの代わりにenterを押して、これを早急に入力してください。 – BudSkywalker

+0

このコードでは、まずフォルダーが存在するかどうかを確認してから、そのファイルにアクセスします。 * FileNotFoundException *をスローするコードでは、このコードがなく、入力ストリームとしてロードしようとした直後に* images *フォルダを作成しようとしました。おそらく、アクセスしようとしているフォルダが存在するかどうかを確認してください。 –

答えて

1

は私の質問に答えコードです:

public static void writeSaves(File src, File dest) throws IOException { 
    if(src.isDirectory()){ 
     //if directory not exists, create it 
     if(!dest.exists()){ 
      dest.mkdirs(); 
     } 

     //list all the directory contents 
     String files[] = src.list(); 

     for (String file : files) { 
      //construct the src and dest file structure 
      File srcFile = new File(src, file); 
      File destFile = new File(dest, file); 
      //recursive copy 
      writeSaves(srcFile,destFile); 
     } 

    }else{ 
     //if file, then copy it 
     //Use bytes stream to support all file types 

     InputStream in = new FileInputStream(src); 
      OutputStream out = new FileOutputStream(dest); 

      byte[] buffer = new byte[1024]; 

      int length; 
      //copy the file content in bytes 
      while ((length = in.read(buffer)) > 0){ 
       out.write(buffer, 0, length); 
      } 

      in.close(); 
      out.close(); 
    } 

はあなたを助けるためにしようとしたすべての人に感謝します!

1

C:\Users\censored\Documents\Electrocode Productions\template\resources\imagesディレクトリをmkdirs呼び出しで作成したようです。次に、明らかに失敗したファイルのように開きます。ここで

+0

Ok。ディレクトリを書き込むために使用できる別のメソッド(xOutputStream)がありますか? – BudSkywalker

+0

ディレクトリ内のファイルの一覧表示、作成、削除、または名前の変更はできません。少なくともWindowsでは、directioをjava.ioで読み書きするためのバイトストリームとして開くことはできません。同じように、リソースディレクトリの読み書きも同じです。複数のファイルをコピーする場合は、@ BudSkywalkerを反復処理する必要があります – eckes

関連する問題