简体   繁体   English

无法读取.csv文件。 从JOptionPane切换到控制台I / O后出现FileNotFoundException错误

[英]Failed to read a .csv file. Getting FileNotFoundException error after switching from JOptionPane to console I/O

Here are two versions of the same method. 这是同一方法的两个版本。 the first one uses JOptionPane and the second one uses console. 第一个使用JOptionPane,第二个使用控制台。 They are supposed to take a file path (string) from user input to find the file and read it. 他们应该从用户输入中获取文件路径(字符串)以查找文件并读取它。

The first method works just fine but the second method which uses console gave me a FileNotFoundException error. 第一种方法效果很好,但是第二种使用控制台的方法却给了我FileNotFoundException错误。 Why doesn't the second one work if they both are almost identical? 如果第二个几乎相同,为什么第二个却不起作用?

//USING JOptionPane //////////////////////////////////////////////////////////////// //使用JOptionPane ////////////////////////////////////////////// //////////////////

public void addFromFile() { 公共无效addFromFile(){

    String[] option1 = { "Go back to Main Manu", "Continue" };

    int choice4 = JOptionPane.showOptionDialog(null,
            "Warning: this method will delete existing data before it add file data", "Monster Database",
            JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, option1, null);

    if (choice4 == 0)
        monitor();

    if (choice4 == 1) {

        monsterAttackList.clear();

        Scanner input = new Scanner(System.in);
        String filePath = JOptionPane.showInputDialog(null, "Enter a filepath");

        File inFile = new File(filePath);

        try {
            Scanner fileReader = new Scanner(inFile);

            String line;
            String[] part;
            int attackID;
            String monster;
            String date;
            String location;
            String reporter;

            while (fileReader.hasNextLine()) {
                line = fileReader.nextLine();
                part = line.split(",");
                attackID = Integer.parseInt(part[0]);
                monster = part[1];
                date = part[2];
                location = part[3];
                reporter = part[4];

                monsterAttackList.add(new MonsterAttack(attackID, monster, date, location, reporter));
            }

            fileReader.close(); // Close to unlock.
            input.close();

        } catch (IOException e) {
            JOptionPane.showMessageDialog(null, "filepath is not valid");
            System.err.println(e);

        }
    }

}   

// USING SCANNER /////////////////////////////////////////////////////////////////////////// //使用扫描仪/////////////////////////////////////////////// /////////////////////////////

public void addFromFile() {

    System.out.println("Warning: this method will delete existing data before it add file data");
    System.out.println("\n1. Go back to Main Manu" + "\n2. Continue \n");

    Scanner input = new Scanner(System.in);
    int choice4 = input.nextInt();


    if (choice4 == 1)
        monitor();

    if (choice4 == 2) {

        monsterAttackList.clear();

        System.out.println("Enter a filepath");

        String filePath = input.nextLine();

        File inFile = new File(filePath);

        try {
            Scanner fileReader = new Scanner(inFile);

            String line;
            String[] part;
            int attackID;
            String monster;
            String date;
            String location;
            String reporter;

            while (fileReader.hasNextLine()) {
                line = fileReader.nextLine();
                part = line.split(",");
                attackID = Integer.parseInt(part[0]);
                monster = part[1];
                date = part[2];
                location = part[3];
                reporter = part[4];

                monsterAttackList.add(new MonsterAttack(attackID, monster, date, location, reporter));
            }

            fileReader.close(); // Close to unlock.
            input.close();

        } catch (IOException e) {

            System.out.println("filepath is not valid");
            System.err.println(e);

        }
    }

}

You will want to put a call to input.nextLine() after every call to input.nextInt() . 您将想在每次调用input.nextLine()之后调用一次input.nextInt() NextInt does not consume the newline character, only the number. NextInt不使用换行符,仅使用数字。

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

相关问题 Apache Beam管道从csv文件读取,拆分,groupbyKey并写入文本文件时出现“ IllegalStateException”错误。 为什么? - “IllegalStateException” error for Apache Beam pipeline to read from csv file, split, groupbyKey and write to text file. Why? 从网站读取csv文件返回java.io.FileNotFoundException - Read csv file from website return java.io.FileNotFoundException 我应该如何在不获取 FileNotFoundException 的情况下读取文本文件? - How should I read a text file without getting FileNotFoundException? 尝试从资源目录读取Excel文件时获取FileNotFoundException - getting FileNotFoundException when trying to read an excel file from resource directory 我收到 FileNotFoundException 但文件在那里 - I am getting a FileNotFoundException but the file is there 我想从文件中的一行读取数据。 在Java中 - I want to read data from a line in a file. in Java 错误:无法打开文件。 Android Studio 3 - Error: failed to open file. Android Studio 3 尝试从文件中读取数据后创建HashMap。 获取空值。 我究竟做错了什么? - Trying to create a HashMap after reading data from file. Getting null values. What am I doing wrong? Java:尝试从文本文件读取时获得FileNotFoundException,即使该文件存在 - Java: Getting FileNotFoundException when trying to read from a text file even though the file exists 从文件读取int [] []。 爪哇 - Read int[][] from file. Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM