简体   繁体   English

为什么我的程序无法读取使用同一程序创建的文件?

[英]Why doesn't my program read my file that was created using the same program?

I'm trying to create a program that makes a file that randomly generates numbers and I want the program to read those numbers off of the file and analyze it. 我正在尝试创建一个程序,该程序使文件随机生成数字,并且我希望该程序从文件中读取这些数字并进行分析。 If the randomly generated number doesn't equal 0, the program should keep generating numbers, but if it does equal 0, then the program will stop. 如果随机生成的数字不等于0,则程序应继续生成数字,但如果确实等于0,则程序将停止。 However, it seems that my program is not reading those numbers. 但是,看来我的程序没有读取这些数字。

I tried putting the outFile.close(); 我试着把outFile.close(); and inFile.close(); inFile.close(); in a couple different places to see if that would fix anything, but it seems that didn't work out. 在几个不同的地方看看这是否可以解决问题,但似乎没有解决。 I tried tracing my code with pen and paper, but I couldn't find anything wrong. 我尝试用笔和纸来跟踪代码,但找不到任何错误。 Perhaps it could be my placement of outFile.close(); 也许是我放置outFile.close(); and inFile.close(); inFile.close(); , but I couldn't find anything wrong with it. ,但我找不到任何问题。

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

public class squirrel {

public static void main(String[] args) throws IOException{
    Scanner in = new Scanner(System.in);
    PrintWriter outFile = new PrintWriter(new File("squirrel.txt"));
    Scanner inFile =  new Scanner(new File("squirrel.txt"));
    Random rand = new Random();
    int squirrelNum;
    int foxSquirrel = 0;
    int squirrelsSeen = 0;
    int trials = 0;

    System.out.println("Welcome to the Fox Squirrel Simulator\n");
    System.out.println("How many trials should be simulated?");
    System.out.println("Enter a value greater than 1000: ");
    trials = in.nextInt();

    while(trials <= 1000)
    {
        System.out.println("Please try again. Enter a value greater than 1000: ");
        trials = in.nextInt();
    }

    for(int i = 0; i <= trials; i ++)
    {
        squirrelNum = rand.nextInt(10);
        outFile.println(squirrelNum);

        while(inFile.hasNextInt())
        {
            int token = inFile.nextInt();

            while(token != 0)
            {

                squirrelsSeen ++;
            }

            if(token == 0)
            {
                foxSquirrel ++;
                squirrelsSeen ++;
            }
            outFile.close();
        }


        System.out.println("squirrelsSeen: " + squirrelsSeen);
        System.out.println("foxSquirrel: " + foxSquirrel);
    }


    inFile.close();
    System.out.println("\nsimulating trials now... one moment please...\n");
    System.out.println("The results!");
    System.out.println("The average number of squirrels until spotting a Fox Squirrel at the city park is: " + (((double)foxSquirrel / squirrelsSeen) * 100));



 }

}

And here is how it is done using Scanner. 这是使用扫描仪完成的方法。

File file = new File("squirrel.txt");

    try {

        Scanner sc = new Scanner(file);

        while (sc.hasNextLine()) {
            int i = sc.nextInt();
            System.out.println(i);
        }
        sc.close();
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }

And it is like you said. 就像你说的那样。 You put the .close() inside your while loop in your code. 您将.close()放入代码的while循环中。 Try putting it outside. 尝试将其放在外面。

new PrintWriter(new File("squirrel.txt")) immediately erases the file. new PrintWriter(new File("squirrel.txt"))立即删除该文件。 From the documentation : 文档中

If the file exists then it will be truncated to zero size; 如果文件存在,那么它将被截断为零大小; otherwise, a new file will be created. 否则,将创建一个新文件。

It is not possible to read and write the same file simultaneously. 无法同时读取和写入同一文件。 (Actually there are some cases were it's possible, but they don't apply here.) (实际上,有一些情况是可能的,但在这里并不适用。)

Do not create your PrintWriter until you have finished reading from the file and have called inFile.close() . 在完成从文件读取并调用inFile.close()之前,请勿创建PrintWriter。 Or, write to a different file, then when you're done, rename it to match the original file. 或者,写一个不同的文件,完成后,将其重命名以匹配原始文件。

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

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