package Memento;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
ReadPhrase readPhrase = new ReadPhrase(new File("D:/09soft/MyEclipse 10/yanning/src/Memento/phrase.txt"));
File favorPhrase = new File("favorPhrase.txt");
RandomAccessFile out = null;
try {
out = new RandomAccessFile(favorPhrase, "rw");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("是否从上次读取的位置继续读取成语(输入y或n)");
String answer = reader.nextLine();
if (answer.startsWith("y")||answer.startsWith("Y")) {
Caretaker caretaker = new Caretaker();
Memento memento = caretaker.getMemento();
if (memento != null) {
readPhrase.restoreFromMemento(memento);
}
String phrase = null;
while((phrase = readPhrase.readLine()) != null) {
System.out.println(phrase);
System.out.println("是否将该成语保存到" + favorPhrase.getName());
answer = reader.nextLine();
if (answer.startsWith("y")||answer.startsWith("Y")) {
try {
out.seek(favorPhrase.length());
byte[] b = phrase.getBytes();
out.write(b);
out.write(' ');
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("是否继续读取成语?(输入y或n)");
answer = reader.nextLine();
if (answer.startsWith("y")||answer.startsWith("Y")) {
continue;
} else {
readPhrase.closeRead();
caretaker.saveMemento(readPhrase.createMemento());
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
System.exit(0);
}
}
System.out.println("读完全部成语");
}
System.exit(0);
}
}