简体   繁体   English

读写文件无法正常工作

[英]Reading and writing to file not working correctly

I have been working on this code for the day and am almost at the finish line. 我今天一直在编写此代码,几乎快要完成了。 What I want is that the code should work as a clip card, remembering the number of purchased coffees, and awarding the customer a free coffee every 10th purchase. 我想要的是该代码应作为剪贴卡使用,记住已购买的咖啡数量,并每10次购买向客户奖励一次免费的咖啡。 I'm writing to a file and reading from it in order for a customer to be able to continue his clip card where he left of last time. 我正在写文件并从中读取文件,以便客户能够将他的剪辑卡继续到他上次离开的位置。 So to my problem...I have properly been able to write my "count" variable to a file, and it is storing it correctly. 因此,对于我的问题...我已经能够正确地将“ count”变量写入文件,并且它可以正确存储它。 However, every time I run the program again it starts off a 0 and I don't see why. 但是,每次我再次运行该程序时,它都从0开始,我不明白为什么。 I need it to save the current count, and read the count once run again. 我需要它来保存当前计数,并再次运行后读取计数。 For example, if a customer has previously purchased 7 coffees and is returning to the store, his counter needs to start at 7. For some reason it is not doing that. 例如,如果客户以前购买了7杯咖啡,然后要返回商店,则他的柜台需要从7点开始。由于某种原因,它没有这样做。

Here's what I have so far: 这是我到目前为止的内容:

public class FelixNeww {

    public static void main(String [] args) {
        Scanner key;
        String entry;
        int count = 0;
        String password = "knusan01";

        FelixNeww  f = new FelixNeww();

       System.out.println(f.readFromFile());

        while(true) {
            System.out.println("Enter password: ");
            key = new Scanner(System.in);
            entry = key.nextLine();
            if(entry.compareTo(password) == 0){
                count++;
                System.out.println("You're one step closer to a free coffe! You have so far bought " 
                        + count + " coffe(s)");
                f.saveToFile(count);
            }
            if(count == 10  && count != 0){
                System.out.println("YOU'VE GOT A FREE COFFE!");
                count = 0;
            }
            if(entry.compareTo(password) != 0){
                System.out.println("Wrong password! Try again.\n");
            }
        }



    }

    public void saveToFile(int count)
    {
        BufferedWriter bw = null;
        try
        {
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("C:\\Temp\\countStorage.txt"))));
            bw.write(Integer.toString(count));
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(bw != null)
            {
                try
                {
                    bw.close();
                }
                catch(IOException e) {}
            }
        }
    }

    public int readFromFile()
    {
        BufferedReader br = null;
        try
        {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("C:\\Temp\\countStorage.txt"))));
            String line = br.readLine();
            int count = Integer.parseInt(line);
            return count;
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(br != null)
            {
                try
                {
                    br.close();
                }
                catch(IOException e) {}
            }
        }
        return 0;
    }


}

You are currently setting your count variable to 0. You should set it to the value that's in the file. 当前,您正在将count变量设置为0。应将其设置为文件中的值。 Do this just before the while loop: while循环之前执行此操作:

count = f.readFromFile();
while(true) {

You should also implement a way to gracefully exit the while loop. 您还应该实现一种方式来优雅地退出while循环。 For example, if the user enters "q", you can execute the break; 例如,如果用户输入“ q”,则可以执行break; statement to exit the while loop. 语句退出while循环。 And after your while loop, call key.close(); while循环之后,调用key.close(); to close the Scanner object. 关闭扫描仪对象。

The scope of count variable is local in both instances 在两种情况下,count变量的范围都是局部的

public static void main(String [] args) {
    Scanner key;
    String entry;
    int count = 0;
    String password = "knusan01";


   System.out.println(f.readFromFile());

public int readFromFile()
{
    BufferedReader br = null;
    try
    {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("C:\\Temp\\countStorage.txt"))));
        String line = br.readLine();
        int count = Integer.parseInt(line);
        return count;

In the readFromFile function, you read it from the file, return it, but don't keep track of it in a variable, why don't you replace the println with this inside your main: 在readFromFile函数中,您从文件中读取它,将其返回,但不要在变量中跟踪它,为什么不在内部main中用它替换println:

count=f.readFromFile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM