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

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

问题描述:

以下是同一方法的两个版本。第一个使用JOptionPane,第二个使用控制台。他们应该从用户输入中获取文件路径(字符串)以查找文件并读取它。无法读取.csv文件。从JOptionPane切换到控制台I/O后出现FileNotFoundException错误

第一种方法工作得很好,但是使用控制台第二种方法给了我一个FileNotFoundException异常错误。为什么第二个如果两者几乎完全一样就不工作?

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

公共无效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); 

     } 
    } 

} 

你会希望把调用input.nextLine()每次致电input.nextInt()后。 NextInt不使用换行符,只使用数字。

+0

扫描仪输入=新扫描仪(System.in); \t \t int choice4 = input.nextInt(); \t \t if(choice4 == 1) \t \t \t monitor(); \t \t if(choice4 == 2){ \t \t \t input.nextLine(); //*线 \t \t \t monsterAttackList.clear(); \t \t \t的System.out.println( “输入文件路径”); \t \t \t字符串文件路径= input.nextLine(); \t \t \t文件INFILE =新的文件(文件路径); – Neo

+0

^我如何让它看起来像一个代码块?感谢您的帮助,但我仍然得到同样的错误。 – Neo