繁体   English   中英

如何通过扫描仪读取文本文件

[英]How To Read A Text File Via Scanner

我正在尝试从文本文件中读取内容,但是只要程序进入我的while循环,它就会跳过它。 我使用了前面的示例,我必须检查一下我是否正确执行了操作,但是这次似乎不起作用。

编辑:澄清一下,其下带有“ b ++”的那一刻被跳过。

编辑2:更新的代码。

    public static void main(String[] args) throws FileNotFoundException {

    ToDoItem td = new ToDoItem();
    ToDoList tl = new ToDoList();

    File file = new File("ToDoItems.txt");
    Scanner ReadFile = new Scanner(file);
    Scanner keyboard = new Scanner(System.in);

    ArrayList<String> list = new ArrayList<>();
    String inputline;

    System.out.println("Welcome to the list maker!" + "\n" + "Please start typing.");

    try (PrintWriter fout = new PrintWriter(new File("ToDoItems.txt"))) {

        do {
            System.out.println("add to the list? [y/n]");
            inputline = keyboard.nextLine();

            if ("y".equals(inputline)) {
                fout.print(td.getDescription() + "\n");

            } else {
                System.out.println("Here is the list so far:");

                while (ReadFile.hasNext()) {
                    String listString = ReadFile.nextLine();
                    list.add(listString);

                }
            }
        } while ("y".equals(inputline));

    } catch (FileNotFoundException e) {
        System.err.println(e.getMessage());
    }
    System.out.println(list);
}

理想情况下,我希望它每次通过while循环时都打印Array的一部分。 但这最终只是跳过它。 我检查了文本文件本身,它确实包含要打印的信息。 但是由于某种原因,扫描仪无法正确读取。

String[] stringArray = new String[b]; 因为您的int b = 0;是有问题的int b = 0;

另外,似乎您甚至不知道数组的大小。 我建议您改用ArrayList 这样,您将不需要计数器,只需将其添加到ArrayList中即可。

try catch您的FileNotFoundException而不是扔到main会更好,但是我想您知道您的文件将一直存在。

我想您的问题是您正在尝试读取当前正在使用的文件,请在读取前关闭fout对象,如下所示:

public static void main(String[] args){
    File file = new File("ToDoItems.txt");
    ToDoItem td = new ToDoItem();
    ToDoList tl = new ToDoList();

    Scanner keyboard = new Scanner(System.in);

    ArrayList<String> list = new ArrayList<>();
    String inputline;

    System.out.println("Welcome to the list maker!" + "\n" + "Please start typing.");
    try (PrintWriter fout = new PrintWriter(file)) {
    //                                       ^^ here
        do {
            System.out.println("add to the list? [y/n]");
            inputline = keyboard.nextLine();

            if ("y".equals(inputline)) {
                fout.print(td.getDescription() + System.lineSeparator());
            } else {
                // Important line is here!
                fout.close(); // <--- Close printwriter before read file

                System.out.println("Here is the list so far:");
                Scanner ReadFile = new Scanner(file);

                while (ReadFile.hasNext()) {
                    String listString = ReadFile.nextLine();
                    list.add(listString);
                }
            }
        } while ("y".equals(inputline));
    } catch (FileNotFoundException e) {
        System.err.println(e.getMessage());
    }
    System.out.println(list);
}

暂无
暂无

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

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