无法从命令行读取文件。这些文件位于同一文件夹中。 FileNotFountException

时间:2015-06-10 00:20:52

标签: java file

我写这篇文章是为了从txt文件中获取有关书籍的数据。 (ISBN,Price,Stock)我一直遇到FileNotFoundException问题,无法找出问题所在。对不起,如果代码不好或者我现在仍然在编程。

import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.io.File;

public class BookInventory {
   static ArrayList ISBN = new ArrayList();
   static ArrayList Price = new ArrayList();
   static ArrayList Stock = new ArrayList();      

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

      if(handleArgs(args)){
         for(int j = 0; j < args.length; j++){
            getInventory(args, j);
         }
      }  
   }

   public static void getInventory(String[]args, int j)throws FileNotFoundException{
      Scanner input = null;
      try {
         File inputFile = new File(args[j]); 
         Scanner booksFile = new Scanner(inputFile);      
         if(booksFile.hasNextDouble()){  

            while (booksFile.hasNext()) { 
               try {
                  ISBN.add(booksFile.next());
                  Price.add(booksFile.next());
                  Stock.add(booksFile.next());
               } catch (java.util.InputMismatchException ex) {
                  throw ex;
               }
            }
         }
      } catch (FileNotFoundException ex) {
         System.out.println("Error: File " + args[j] + " not found. Exiting   program.");
         System.exit(1);
      }
   }
   public static boolean handleArgs(String[]args){
      if(args.length < 2){           
         System.out.println("Incorrect number of command line arguments.");
         System.out.println("Program requires at least 2 files.");
         return false;
      }          
      return true; 
   } 
}

0 个答案:

没有答案