Java에서 약속 도서 프로그램을 만들려고하는데 BufferedWriter가 파일에 쓰지 않는 것 같습니다.BufferedWriter를 텍스트 파일에 쓰려면 어떻게해야합니까?
이 코드 블록 try { } catch (FileNotFoundException) { } catch (IOException) { }
FileWriter writer = new FileWriter("Appointments" + name + ".txt", true);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
if (open == 'o') {
FileReader reader = new FileReader("Appointments" + name + ".txt");
BufferedReader bufferedReader = new BufferedReader(reader);
while ((line = bufferedReader.readLine()) != null) {
if (line.indexOf("one-time") == line.length() - 8) {
OneTime o = fromStringOneTime(line);
oneTimes.add(o);
}
if (line.indexOf("daily") == line.length() - 5) {
Daily d = fromStringDaily(line);
dailys.add(d);
}
if (line.indexOf("monthly") == line.length() - 7) {
Monthly m = fromStringMonthly(line);
monthlys.add(m);
}
}
}
while 루프가 단지 현재 3 가지 ArrayLists로 파일의 모든 값을 초기화하고 모든 내부이다. 당신이 선택하면 다음
나는 당신이 (변수 desc
및 date
이전에 정의 된) 약속을 추가 할 수있는 새 개체를 만들 수있는 코드 블록을 가지고 :
System.out.print("Enter if this is a (o)ne-time, (d)aily, or (m)onthly appointment: ");
freq = scanner.next().charAt(0);
if (freq == 'o') {
OneTime o = new OneTime(desc, date.getDate());
oneTimes.add(o);
totalCount++;
bufferedWriter.write(o.toString());
bufferedWriter.newLine();
valid = true;
} else if (freq == 'd') {
Daily o = new Daily(desc, date.getDate());
dailys.add(o);
totalCount++;
bufferedWriter.write(o.toString());
bufferedWriter.newLine();
valid = true;
} else if (freq == 'm') {
Monthly o = new Monthly(desc, date.getDate());
monthlys.add(o);
totalCount++;
bufferedWriter.write(o.toString());
bufferedWriter.newLine();
valid = true;
}
의 끝에서 시도 블럭, 나는 라인 reader.close()
과 writer.close()
있습니다. 그렇다면 저장해야하는 파일을 검사 할 때 여전히 공백으로 표시되는 이유는 무엇입니까? 이것은 파일 저장을 다루는 첫 번째 시간이기 때문에 왜 그렇게 작동하지 않는 것인지 확신 할 수 없습니다. .
편집 : 옵션 대신 새로운 하나
동시에 파일을 읽고 쓸 수는 없습니다. 대부분의 운영 체제. – EJP
그렇다면 사용을 마친 직후 독자를 닫으면 작동할까요? 왜냐하면 나는 초기화를 위해서만 사용할 필요가 있기 때문에 코드의 첫 번째 블록에서 나는 내 게시물에 넣는다. – RoboticPlayer
다른 파일 이름으로 파일에 쓰고 프로그램 끝에서 이름을 바꿀 수 있습니까? – GrabNewTech