이 프로그램은 출력 파일 이름을 묻는 메시지를 표시합니다. 텍스트 편집기 나 터미널로 출력 파일을 열려고 할 때까지. 그럼 그 파일에 아무것도 보이지 않는 것뿐입니다. 이 프로그램은 텍스트 파일을 만들지 만 파일은 비어 있습니다. 미리 도움을 주셔서 감사합니다.PrintWriter가 .txt 파일에 텍스트를 인쇄하지 않습니다.
import java.util.*;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Writes a Memo file.
*
*/
public class MemoPadCreator {
public static void main(String args[]) {
Scanner console = new Scanner(System.in);
System.out.print("Enter Output file name: ");
String filename = console.nextLine();
try {
PrintWriter out = new PrintWriter(filename);
boolean done = false;
while (!done) {
System.out.println("Memo topic (enter -1 to end):");
String topic = console.nextLine();
// Once -1 is entered, memo's will no longer be created.
if (topic.equals("-1")) {
done = true;
console.close();
}
else {
System.out.println("Memo text:");
String message = console.nextLine();
/* Create the new date object and obtain a dateStamp */
Date now = new Date();
String dateStamp = now.toString();
out.println(topic + "\n" + dateStamp + "\n" + message);
}
}
/* Close the output file */
} catch (IOException exception) {
System.out.println("Error processing the file:" + exception);
}console.close();
}
}
이것은 성능에있어 정말로 나쁜 생각입니다. –
@MirkoAdari 예 true이지만 현재 프로그램이 성능을 필요로하지 않는 것 같습니다. 그게 내가 왜 제안했는지. 어쨌든 유효한 포인트는 게시물에 포함됩니다. –
감사합니다. –