Java 打印到文本文件上
我创建了一个Java 打印到文本文件上,java,text,printwriter,Java,Text,Printwriter,我创建了一个PrintWriter和一个新的文本文件,并希望将我的答案打印到文本文件中。但是,由于代码嵌入到程序的循环中,因此每次循环重新启动时,都会创建一个新的文本文件来替换上一个文本文件。如何编写代码,使文本文件不会在每次循环开始时重新创建 这是我的密码: public class Wordler { public static void main(String[] args) throws IOException{ //introduces the game
PrintWriter
和一个新的文本文件,并希望将我的答案打印到文本文件中。但是,由于代码嵌入到程序的循环中,因此每次循环重新启动时,都会创建一个新的文本文件来替换上一个文本文件。如何编写代码,使文本文件不会在每次循环开始时重新创建
这是我的密码:
public class Wordler {
public static void main(String[] args) throws IOException{
//introduces the game
System.out.println("Wordler: Finding words within a word");
System.out.println(" ");
System.out.println("Directions:");
System.out.println("Create as many words as you can with the letters in the word given. Once you have written as many words as you can think of, type");
System.out.println("'x' and then hit the enter key to end the game round. Good luck!");
System.out.println("--------------------------------------------------------------------------------------------------------------------------");
runGame();
}
//method to run the game
public static void runGame() throws FileNotFoundException{
//array list of arrays that contains all the possible words
ArrayList<String> wholeList = new ArrayList<String>();
wholeList.add("vulnerability");
wholeList.add("calculate");
wholeList.add("virtual");
//PrintWriter and File
File results = new File("WordlerResults.txt");
PrintWriter output = new PrintWriter(results);
//list of words and their answers (as a sublist)
ArrayList<String> arr1 = new ArrayList<String>();
arr1.add("vulnerabiliy");
arr1.add("ability");
arr1.add("nearby");
arr1.add("lite");
arr1.add("near");
arr1.add("bare");
arr1.add("rule");
arr1.add("bury");
arr1.add("lair");
arr1.add("rile");
arr1.add("bear");
arr1.add("liberality");
arr1.add("virulently");
arr1.add("vulnerably");
arr1.add("inevitably");
arr1.add("tenurially");
arr1.add("inertially");
arr1.add("neutrally");
arr1.add("unlivable");
arr1.add("unitarily");
arr1.add("veniality");
arr1.add("reliantly");
arr1.add("brilliant");
arr1.add("urinative");
arr1.add("nailbiter");
arr1.add("illuviate");
arr1.add("unitively");
arr1.add("veritably");
arr1.add("trivially");
arr1.add("vibratile");
arr1.add("virtually");
// stopped at #20, www.wordplays.com/w/13810606276/vulnerability
List<String> arr1Sub = arr1.subList(1, 30);
ArrayList<String> arr2 = new ArrayList<String>();
arr2.add("calculate");
arr2.add("late");
arr2.add("call");
arr2.add("teal");
arr2.add("talc");
arr2.add("catcall");
arr2.add("tall");
arr2.add("cult");
arr2.add("lace");
arr2.add("tela");
arr2.add("acute");
arr2.add("lacteal");
arr2.add("callet");
arr2.add("acuate");
arr2.add("luteal");
arr2.add("actual");
arr2.add("cullet");
arr2.add("caecal");
arr2.add("alulae");
arr2.add("acetal");
arr2.add("alate");
arr2.add("caeca");
arr2.add("aceta");
arr2.add("eclat");
arr2.add("cecal");
arr2.add("lutea");
arr2.add("cella");
arr2.add("cleat");
arr2.add("tulle");
arr2.add("culet");
arr2.add("alula");
arr2.add("calla");
arr2.add("tale");
arr2.add("tace");
arr2.add("celt");
arr2.add("clue");
arr2.add("alec");
arr2.add("tell");
arr2.add("cull");
arr2.add("alae");
arr2.add("cate");
arr2.add("acta");
arr2.add("tule");
arr2.add("caca");
arr2.add("ceca");
arr2.add("tael");
arr2.add("latu");
arr2.add("lute");
arr2.add("caul");
arr2.add("cute");
arr2.add("luce");
arr2.add("cell");
arr2.add("tala");
List<String> arr2Sub = arr2.subList(1, 52);
ArrayList<String> arr3 = new ArrayList<String>();
arr3.add("virtual");
arr3.add("ritual");
arr3.add("vault");
arr3.add("virtu");
arr3.add("vital");
arr3.add("trial");
arr3.add("rival");
arr3.add("viral");
arr3.add("ultra");
arr3.add("urial");
arr3.add("trail");
arr3.add("aril");
arr3.add("vair");
arr3.add("tali");
arr3.add("virl");
arr3.add("lair");
arr3.add("rail");
arr3.add("airt");
arr3.add("vita");
arr3.add("lati");
arr3.add("vial");
arr3.add("alit");
arr3.add("tail");
arr3.add("lair");
arr3.add("rial");
arr3.add("vatu");
arr3.add("latu");
arr3.add("tirl");
arr3.add("ulva");
arr3.add("litu");
arr3.add("lira");
arr3.add("lari");
arr3.add("vail");
List<String> arr3Sub = arr3.subList(1, 32);
//input list
ArrayList<String> inputList = new ArrayList<String>();
Scanner input = new Scanner(System.in);
//to print the words for the game
int r = (int) (Math.random() * 2);
String word = wholeList.get(r);
System.out.println(word);
while (input.hasNextLine()){
String words = input.nextLine();
if (words.equalsIgnoreCase("x")){
break;
}
else{
inputList.add(words);
}
}
//check answers
ArrayList<String> validAnswers = new ArrayList<String>();
ArrayList<String> wrongAnswers = new ArrayList<String>();
ArrayList<String> notFound = new ArrayList<String>();
List<String> compare = new ArrayList<String>();
if (r == 0){
compare = arr1Sub;
}
else if (r == 1){
compare = arr2Sub;
}
else if(r == 2){
compare = arr3Sub;
}
else{
compare.add("error");
System.out.println(compare);
}
for (int i = 0; i < inputList.size(); i++){
if (compare.contains(inputList.get(i))){
validAnswers.add(inputList.get(i));
}
else if (!compare.contains(inputList.get(i))){
wrongAnswers.add(inputList.get(i));
}
else{
notFound.add(compare.get(i));
}
}
System.out.println("Valid Answers: " + validAnswers);
System.out.println("Wrong Answers: " + wrongAnswers);
output.println(wholeList.get(r));
output.println("Valid Answers: " + validAnswers);
output.println("Wrong Answers: " + wrongAnswers);
output.close();
System.out.println(" ");
System.out.println("Would you like to play again? (Y/N)");
String response = input.nextLine();
System.out.println(" ");
if (response.equalsIgnoreCase("y")){
repeatGame();
}
else if (response.equalsIgnoreCase("n")){
System.out.println(" ");
System.out.println("Thank you for playing!");
}
}
public static void repeatGame(){
try {
runGame();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
公共类Wordler{
公共静态void main(字符串[]args)引发IOException{
//介绍游戏
System.out.println(“Wordler:在单词中查找单词”);
System.out.println(“”);
System.out.println(“方向:”);
System.out.println(“用给定单词中的字母创建尽可能多的单词。一旦你写了尽可能多的单词,键入”);
System.out.println(“'x'然后按回车键结束游戏回合。祝你好运!”);
System.out.println(“----------------------------------------------------------------------------------------------------------------------------------------------------------------”);
runGame();
}
//运行游戏的方法
public static void runGame()引发FileNotFoundException{
//数组包含所有可能单词的数组列表
ArrayList wholeList=新的ArrayList();
完整列表。添加(“漏洞”);
完整列表。添加(“计算”);
添加(“虚拟”);
//打印机和文件
文件结果=新文件(“WordlerResults.txt”);
PrintWriter输出=新的PrintWriter(结果);
//单词及其答案列表(作为子列表)
ArrayList arr1=新的ArrayList();
arr1.添加(“脆弱性”);
arr1.添加(“能力”);
arr1.添加(“附近”);
arr1.添加(“lite”);
arr1.添加(“接近”);
arr1.添加(“裸”);
arr1.添加(“规则”);
arr1.添加(“掩埋”);
arr1.添加(“巢穴”);
arr1.添加(“rile”);
arr1.添加(“熊”);
arr1.添加(“宽松性”);
arr1.添加(“有毒”);
arr1.添加(“易受攻击”);
arr1.添加(“不可避免”);
arr1.添加(“十年”);
arr1.添加(“无误”);
arr1.添加(“中立”);
arr1.添加(“不可居住”);
arr1.添加(“统一”);
arr1.添加(“威尼斯”);
arr1.添加(“可靠”);
arr1.添加(“辉煌”);
arr1.添加(“小便”);
arr1.添加(“咬钉者”);
arr1.添加(“冲积物”);
arr1.添加(“联合”);
arr1.添加(“真实”);
arr1.添加(“微不足道”);
arr1.添加(“振动”);
arr1.添加(“虚拟”);
//停在#20,www.wordplays.com/w/1381006276/vulnerability
列表arr1Sub=arr1.子列表(1,30);
ArrayList arr2=新的ArrayList();
arr2.添加(“计算”);
arr2.添加(“迟交”);
arr2.添加(“调用”);
arr2.添加(“teal”);
arr2.添加(“滑石”);
arr2.添加(“catcall”);
arr2.添加(“高”);
arr2.添加(“邪教”);
arr2.添加(“花边”);
arr2.添加(“影视处”);
arr2.添加(“急性”);
arr2.添加(“乳制品”);
arr2.添加(“被调用方”);
arr2.添加(“acuate”);
arr2.添加(“黄体”);
arr2.添加(“实际”);
arr2.添加(“碎玻璃”);
arr2.添加(“盲肠”);
arr2.添加(“alulae”);
arr2.添加(“缩醛”);
arr2.添加(“alate”);
arr2.添加(“caeca”);
arr2.添加(“aceta”);
arr2.添加(“eclat”);
arr2.添加(“盲肠”);
arr2.添加(“lutea”);
arr2.添加(“单元格”);
arr2.添加(“夹板”);
arr2.添加(“薄纱”);
arr2.添加(“culet”);
arr2.添加(“alula”);
arr2.添加(“calla”);
arr2.添加(“故事”);
arr2.添加(“tace”);
arr2.添加(“凯尔特”);
arr2.添加(“线索”);
arr2.添加(“亚力克”);
arr2.添加(“告知”);
arr2.添加(“剔除”);
arr2.添加(“alae”);
arr2.添加(“cate”);
arr2.添加(“acta”);
arr2.添加(“tule”);
arr2.添加(“caca”);
arr2.添加(“ceca”);
arr2.添加(“tael”);
arr2.添加(“latu”);
arr2.添加(“鲁特琴”);
arr2.添加(“嵌缝”);
arr2.添加(“可爱”);
arr2.添加(“luce”);
arr2.添加(“单元格”);
arr2.添加(“tala”);
列表arr2Sub=arr2.子列表(1,52);
ArrayList arr3=新的ArrayList();
arr3.添加(“虚拟”);
arr3.添加(“仪式”);
arr3.添加(“保险库”);
arr3.添加(“virtu”);
arr3.添加(“重要”);
arr3.添加(“审判”);
arr3.添加(“竞争对手”);
arr3.添加(“病毒”);
arr3.添加(“ultra”);
arr3.添加(“urial”);
arr3.添加(“线索”);
arr3.添加(“假种皮”);
arr3.添加(“vair”);
arr3.添加(“tali”);
arr3.添加(“virl”);
arr3.添加(“巢穴”);
arr3.添加(“铁路”);
arr3.添加(“airt”);
arr3.添加(“vita”);
arr3.添加(“lati”);
arr3.添加(“小瓶”);
arr3.添加(“alit”);
arr3.添加(“尾部”);
arr3.添加(“巢穴”);
3.添加(“里亚尔”);
arr3.添加(“vatu”);
arr3.添加(“latu”);
arr3.添加(“tirl”);
arr3.添加(“ulva”);
arr3.添加(“litu”);
arr3.添加(“里拉”);
arr3.添加(“拉里”);
arr3.添加(“韦尔”);
列表arr3Sub=arr3.子列表(1,32);
//输入列表
ArrayList inputList=新的ArrayList();
扫描仪输入=新扫描仪(System.in);
//为游戏打印文字
int r=(int)(Math.random()*2);
字符串字=wholeList.get(r);
System.out.println(word);
while(input.hasNextLine()){
String words=input.nextLine();
if(字等信号情况(“x”)){
打破
}
否则{
输入列表。添加(单词);
}
}
//核对答案
ArrayList validAnswers=新的ArrayList();
ArrayList ErrorAnswers=新建ArrayList();
ArrayList notFound=新建ArrayList();
列表比较=新建ArrayList();
如果(r==0){
比较=arr1Sub;
}
else如果(r==1){
PrintWriter writer = new PrintWriter(new FileOutputStream(myFile, true));
writer.write("a String");
writer.close();
public static void main(String[] args) throws IOException{
...
File results = new File("WordlerResults.txt");
PrintWriter output = new PrintWriter(results);
runGame(output);
}
public static void runGame(PrintWriter output) throws FileNotFoundException{
...
//also remove these two lines from here
// File results = new File("WordlerResults.txt");
// PrintWriter output = new PrintWriter(results);
}
output.flush();
output.close();