파일과 내용을 복사하기위한 코드를 아래에 만들었습니다.BufferedReader가 파일을 복사하지 않습니다.
static void copyFile(File inFile, String destination) {
if (inFile.isFile()) {
try {
String str = destination + "//" + inFile.getName();
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(inFile),"UTF-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(str), false), "UTF-8"));
String line;
try {
while((line = br.readLine()) != null) {
bw.write(line);
System.out.println(line);
}
} catch (IOException ex) {
Logger.getLogger(JavaApplication10.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (FileNotFoundException ex) {
Logger.getLogger(JavaApplication10.class.getName()).log(Level.SEVERE, null, ex);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(JavaApplication10.class.getName()).log(Level.SEVERE, null, ex);
}
} else if(inFile.isDirectory()) {
String str = destination + "\\" + inFile.getName();
File newDir = new File(str);
newDir.mkdir();
for(File file : inFile.listFiles())
copyFile(file, newDir.getAbsolutePath());
}
}
코드는 예상대로 목적지에 파일을 creaes하지만 .txt
파일이 비어 있습니다. while 루프
bw.write(line);
에 대한 부분은
System.out.println(line);
작품을 작동하지 않습니다.
()가 새 라인을 폐기하는 시도 할 수 있습니다. 파일의 flush() 또는 close()가 없으면 버퍼링 된 모든 데이터가 디스크에 기록되지 않았습니다 (쓰기 버퍼의 목적입니다) –