파일을 java로 복사하고 새 폴더로 이동하려고합니다. 이것은 내가 사용 해본 코드이지만 "지정된 디렉토리에서 (액세스가 거부되었습니다)"라는 오류 메시지가 항상 나타납니다. 이 문제를 해결할 수있는 방법이나 파일을 복사하는 더 좋은 방법이 있습니까? 감사Java에서 파일을 복사하는 방법
try{
File f1 = new File(fpath);
File f2 = new File("C:/users/peter/documents/foldertest2/hats");
InputStream in = new FileInputStream(f1);
//For Append the file.
//OutputStream out = new FileOutputStream(f2,true);
//For Overwrite the file.
OutputStream out = new FileOutputStream(f2);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0){
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
}
catch(FileNotFoundException ex){
System.out.println(ex.getMessage() + " in the specified directory.");
System.exit(0);
}
catch(IOException e){
System.out.println(e.getMessage());
}
업데이트 : 폴더 권한을 확인하고 모든 사용자에 대해 모두 열려 있고 내
는 소리. 읽으려고하는 파일의 소유자 또는 작성하려는 디렉토리에 대해 충분한 권한을 부여 받도록 요청하는 것이 좋습니다. – duffymo
질문 목록 : fpath는 어떻게 생겼습니까? 모자가 있습니까?그것은 디렉토리입니까? – josefx
fpath is = "C :/users/peter/desktop/hat3", 모자는 존재하며 내 문서 폴더의 폴더 "foldertest2"에있는 폴더입니다. 내가 다른 폴더를 시도하고 여전히 작품을 나던 – Peter