Java 文件处理问题

Java 文件处理问题,java,printstream,Java,Printstream,我是一名java程序员新手,在java项目的文件输出方面遇到了问题。在这个项目中,我应该有一个石头、布、剪刀的程序,它将结果输出到一个单独的文件中。当我运行程序,然后查看文件时,它只记录最近的结果,而不是每个结果。任何关于做什么的建议都会很好。请原谅这张糟糕的表格;我稍后会把它清理干净。我还删除了大部分评论,以缩短它。谢谢 import java.util.*; import java.awt.*; import java.io.*; public class RockPaperScisso

我是一名java程序员新手,在java项目的文件输出方面遇到了问题。在这个项目中,我应该有一个石头、布、剪刀的程序,它将结果输出到一个单独的文件中。当我运行程序,然后查看文件时,它只记录最近的结果,而不是每个结果。任何关于做什么的建议都会很好。请原谅这张糟糕的表格;我稍后会把它清理干净。我还删除了大部分评论,以缩短它。谢谢

import java.util.*;
import java.awt.*;
import java.io.*;


public class RockPaperScissors {


    public static int count = 0;

    public static void main(String[] args) {

            execute(); 
    }


    public static void execute(){       

        System.out.println("This program will allow you to play \n\"Rock, Paper, Scissors\" against a computer.");
        System.out.println();
        System.out.println("Enter 'r' for Rock, 'p' for Paper, or s for Scissors.");
        System.out.println("Enter 'w' to have an insta-win for that round. Enter '-1' at anytime to exit program.");

        String info = userInput();
        int value = guessCode();
        decideOutcome(value, info);
        again();
        }


        public static String userInput() {
        Scanner console = new Scanner (System.in);  
        String s = console.next();
        return s;
    }

        public static int guessCode() {
        Random r = new Random ();   
        return (r.nextInt(3)+1); // Random integer between 1 and 3;
    }

    public static void decideOutcome(int i, String j) {

        try {
            PrintStream output = new PrintStream(new File ("records.txt"));

            if (j.equalsIgnoreCase("rock")|| j.equalsIgnoreCase("r")) {
                count++;
                switch (i){
                    case 1:
                        System.out.println("You've won! Computer picked scissors.");
                        output.println(count + " Win ");
                        break;
                    case 2:
                        System.out.println("You've tied.... Computer also picked rock.");
                        output.println(count + " Tie ");
                        break;
                    case 3:
                        System.out.println("You've lost. Computer picked paper.");
                        output.println(count + " Loss ");
                        break;
                    }           
            } else if (j.equalsIgnoreCase("paper")|| j.equalsIgnoreCase("p")) {
                count++;
                switch (i){
                    case 1:
                        System.out.println("You've lost; Computer picked scissors.");
                        output.println(count + " Loss ");
                        break;
                    case 2:
                        System.out.println("You've won! Computer picked rock.");
                        output.println(count + " Win ");
                        break;
                    case 3:
                        System.out.println("You've tied.... Computer also picked paper.");
                        output.println(count + " Tie ");
                        break;
                    }
            } else if (j.equalsIgnoreCase("scissors")|| j.equalsIgnoreCase("s")) {
                count++;
                switch (i){
                    case 1:
                        System.out.println("You've tied.... Computer picked scissors.");
                        output.println(count + " Tie ");
                        break;
                    case 2:
                        System.out.println("You've lost; Computer picked rock.");
                        output.println(count + " Loss ");
                        break;
                    case 3:
                        System.out.println("You've won! Computer also picked paper.");
                        output.println(count + " Win ");
                        break;
                    }
            } else if (j.equalsIgnoreCase("w")) {
                count++;
                System.out.println("You've effortlessly defeated the computer!");
                output.println(count + " Win ");
            } else if (j.equals("-1")) {
                System.out.println("Thanks for playing!"); // need to find way to reach end.

                if (count == 1) { // If the user terminates after the first match.
                    System.out.println("You've played a single match.");        
                } else if (count > 1) { // Anything more than 1 match played upon termination.
                    System.out.println("You've played " + count + " matches total.");   
                } else { // This is for exceptions when user inputs gibberish for their sign and then 'no' for the second input.
                    System.out.println("No matches were played.");  
            }           
                System.out.println("Good Bye!");
                System.exit(0);
            } else {
                System.out.println("You didn't input the right thing.");
            }
        } catch (FileNotFoundException e) {
                System.out.println("File was not found; try again");
        }
    }

    public static void again() {
            System.out.println("Do you want to play again? (Type in 'y' for Yes or 'n' for No.)");

            Scanner console2 = new Scanner (System.in);

            String t = console2.next();

            while (t.equalsIgnoreCase("yes")||t.equalsIgnoreCase("y")) {
                System.out.println();
                System.out.println();
                execute(); // 
            }  
            if (t.equalsIgnoreCase("no") || t.equalsIgnoreCase("n") || t.equals("-1")) {
                System.out.println("Hope you had fun! I'm sure I've had just as much fun with making this program! Good Bye!");         

                if (count == 1) { // If the user terminates after the first match.
                        System.out.println("You've played a single match.");        
                } else if (count > 1) { // Anything more than 1 match played upon termination.
                        System.out.println("You've played " + count + " matches total.");   
                } else { // This is for exceptions when user inputs gibberish for their sign and then 'no' for the second input.
                        System.out.println("No matches were played.");              
                }   
                System.exit(0);

            } else { // If the user doesn't input 'yes' or 'no.'
                System.out.println("Not the proper response, but it's assumed that you don't want to continue.");   
                if (count == 1) { // If the user terminates after the first match.
                    System.out.println("You've completed a single match."); 
                } else if (count >= 2) { // Anything more than 1 match played upon termination.
                    System.out.println("You've completed " + count + " matches total.");
                } else { // The user haphazardly messes up both inputs.
                    System.out.println("No matches were finished.");
                }
                System.exit(0);
            }
        }
}
当您决定输出时,每次都会重新打开文件的打印流

但是这个构造器不会一直搜索到文件的末尾!这意味着您每次都会覆盖内容

请尝试使用具有适当构造函数的FileWriter

编辑:由于作业似乎要求您使用PrintStream why?,因此您必须这样做:

PrintStream output = new PrintStream(new FileOutputStream("records.txt", true));
但在现实生活中,您可能会使用缓冲写入程序;几乎没有人使用PrintStream。

当您决定退出时,每次都会重新打开文件的PrintStream

但是这个构造器不会一直搜索到文件的末尾!这意味着您每次都会覆盖内容

请尝试使用具有适当构造函数的FileWriter

编辑:由于作业似乎要求您使用PrintStream why?,因此您必须这样做:

PrintStream output = new PrintStream(new FileOutputStream("records.txt", true));

但在现实生活中,您可能会使用缓冲写入程序;几乎没有人使用PrintStream。

您正在使用的PrintStream从一开始就开始写入文件,而不是从最后一行的位置开始写入。 最好使用Filewriter处理文件,因为它有附加和插入模式。
您需要的是追加模式

您正在使用的Printstream从一开始就开始写入文件,而不是从最后一行开始写入。 最好使用Filewriter处理文件,因为它有附加和插入模式。 您需要的是追加模式

您永远无法使用PrintStream以您正在执行的方式完成此任务。API明确说明PrintStream的构造函数执行以下操作:

打印流文件 使用指定的文件创建新的打印流,无需自动换行

文件-用作此打印流目标的文件。如果 文件存在,则会将其截断为零大小;否则,将创建一个新文件。输出将写入文件并进行缓冲


没有允许您附加到上一个文件的构造函数

因此,解决方案在于您只能使用PrintStream构造函数一次。这可以通过将输出变量设置为类变量,并去掉声明和try-catch-in-decideOutcome来实现

另一个需要注意的重要事项是,当您打开诸如扫描仪或打印流之类的流时,应始终将其关闭。关闭它们的最佳位置是finally子句,因为这部分代码保证可以运行。

您永远不能使用PrintStream以您正在执行的方式完成此任务。API明确说明PrintStream的构造函数执行以下操作:

打印流文件 使用指定的文件创建新的打印流,无需自动换行

文件-用作此打印流目标的文件。如果 文件存在,则会将其截断为零大小;否则,将创建一个新文件。输出将写入文件并进行缓冲


没有允许您附加到上一个文件的构造函数

因此,解决方案在于您只能使用PrintStream构造函数一次。这可以通过将输出变量设置为类变量,并去掉声明和try-catch-in-decideOutcome来实现


另一个需要注意的重要事项是,当您打开诸如扫描仪或打印流之类的流时,应始终将其关闭。关闭它们的最佳位置是finally子句,因为这部分代码保证运行。

fge已经回答了这个问题,所以我不会创建一个答案,但您的代码有一个非常糟糕的地方:execute正在再次调用execute,而execute正在再次调用,。。。这被称为递归,如果玩家想再次玩很多次,函数调用会堆积起来,你会遇到堆栈溢出错误。你应该使用一个循环。fge已经回答了这个问题,所以我不会创建一个答案,但是你的代码有一个非常糟糕的地方:execute再次调用execute再次调用execute,。。。这被称为递归,如果玩家想再次玩很多次,函数调用会堆积起来,你会遇到堆栈溢出错误。你应该用循环代替。我不熟悉循环
文件编写器。即使如此,我认为我必须使用Printstream作为我的作业要求的一部分。有没有办法保持PrintStream方法?感谢您的帮助。在本例中,请在附加模式下打开文件的FileOutputStream,并使用适当的PrintStream构造函数ie,该构造函数将OutputStream作为参数。感谢您的帮助。成功了。我必须使用PrintStream的原因是,项目是分阶段提交的。每一阶段都要求学生在课程进行的过程中实施阅读中的元素。这项任务将于周二晚上完成,要求我实现文件输出功能以及总体改进。然而,关于文件输出的章节只谈到使用PrintStream进行文件输出,因此我必须使用它来表明我已经完成了阅读。我不熟悉FileWriter。即使如此,我认为我必须使用Printstream作为我的作业要求的一部分。有没有办法保持PrintStream方法?感谢您的帮助。在本例中,请在附加模式下打开文件的FileOutputStream,并使用适当的PrintStream构造函数ie,该构造函数将OutputStream作为参数。感谢您的帮助。成功了。我必须使用PrintStream的原因是,项目是分阶段提交的。每一阶段都要求学生在课程进行的过程中实施阅读中的元素。这项任务将于周二晚上完成,要求我实现文件输出功能以及总体改进。但是,关于文件输出的一章只讨论了使用PrintStream进行文件输出,因此我必须使用它来表明我已经完成了阅读。没有允许您附加到上一个文件的构造函数+好吧,你技术上是对的。让我重新表述一下,我的意思是,如果要实现附加,就不能将PrintStream用作唯一的类。似乎这是他想要/必须用于分配作业的唯一类。没有允许您附加到上一个文件的构造函数+好吧,你技术上是对的。让我重新表述一下,我的意思是,如果要实现附加,就不能将PrintStream用作唯一的类。看来这是他想/必须用来完成作业的唯一一门课。