몇 가지 이유로, 내 프로그램에 새 BufferedWriter 및 FileWriter를 만들 때 (아직 아무 것도 쓰지 않았더라도) 모든 텍스트를 선택한 파일이 지워집니다.어떤 이유로 FileWriter 및 BufferedWriter 지우기 파일을 사용합니까?
selectedFile은 JFileChooser에 의해 결정됩니다.
public static File selectedFile;
public static void Encrypt() throws Exception {
try {
//if I comment these two writers out the file is not cleared.
FileWriter fw = new FileWriter(selectedFile.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
List<String> lines = Files.readAllLines(Paths.get(selectedFile.toString()),
Charset.defaultCharset());
for (String line : lines) {
System.out.println(line);
System.out.println(AESencrp.encrypt(line));
/*file is cleared regardless of whether or not these are commented out or
* not, as long as I create the new FileWriter and BufferedWriter the file
* is cleared regardless.*/
//bw.write(AESencrp.encrypt(line));
//bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
AESencrp.encrypt는 내가 가지고있는 암호화 클래스 일 뿐이며 영향을 미치지 않습니다. 새로운 FileWriter와 BufferedWriter를 만들면이 루프도 실행되지 않습니다. (적어도 필자는 그렇게 믿지 않습니다. 필자는 줄의 암호화 나 파일의 원래 내용을 얻지 못했고, t는 새로운 FileWriter/BufferedWriter의를 만들었습니다.)
for (String line : lines) {
System.out.println(line);
System.out.println(AESencrp.encrypt(line));
/*file is cleared regardless of whether or not these are commented out or
* not, as long as I create the new FileWriter and BufferedWriter the file
* is cleared regardless.*/
//bw.write(AESencrp.encrypt(line));
//bw.close();
}