【问题标题】:How to use FileInputStream for accessing an input file from another class?如何使用 FileInputStream 从另一个类访问输入文件?
【发布时间】:2016-09-05 03:33:41
【问题描述】:

这是两个 java 类:

获取输入文件:

public class Inputfile {    

public static void main(String[] args) throws Exception  {
    Scanner sc = new Scanner(System.in);
    Scanner input = null;
    boolean isFile = false;
    while (isFile == false){   
        System.out.print("Input file name? ");
        String fileName = sc.next();

        File inputFile = new File(fileName);
        if (inputFile.exists()){
            input = new Scanner(inputFile);
            isFile = true;
        }            
    } 

解析输入文件:

public class PdfParse {

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



Inputfile inputfile = new Inputfile();
 String fileName;
 fileName =  inputfile.toString();      
  FileInputStream inputstream = new FileInputStream(new File("fileName"));


ParseContext pcontext = new ParseContext();
  .........   
  } 

我得到的只是文件名的 FileNotfound 异常。 我尝试使用字符串名称,但无法使用 getter 从 inputfile 类中获取字符串名称,但我失败了。谁能告诉我该怎么做?

非常感谢。

【问题讨论】:

    标签: java input file-io inputstream user-input


    【解决方案1】:

    您可以将文件传递给第二类:

    Scanner sc = new Scanner(System.in);
    File inputFile = null;
    while (inputFile == null){   
        System.out.print("Input file name? ");
        String fileName = sc.next();
    
        inputFile = new File(fileName);
        if (!inputFile.exists()){
            inputFile = null;
        }            
    }
    
    PdfParse.parse(input); // <-- Pass file to parser
    

    那么您的 PdfParse 类可能如下所示:

    public class PdfParse {
    
        public static void parse(File inputFile) throws Exception {
            FileInputStream inputstream = new FileInputStream(inputFile);
            ...
        }
    
    } 
    

    【讨论】:

    • 谢谢,乔恩!但另一个答案更简单。 :)
    【解决方案2】:

    您可以在 Inputfile 类中定义您的 getFileName 方法

     public class Inputfile {    
    
          public static String getFileName() throws Exception  {
            Scanner sc = new Scanner(System.in);
            String fileName = null;
            boolean isFile = false;
            while (!isFile){   
                System.out.print("Input file name? ");
                fileName = sc.next();
    
                File inputFile = new File(fileName);
                if (inputFile.exists()){
                    isFile = true;
                }            
            } 
            return fileName;
        }
    }
    

    然后你可以在PdfParse类的main方法中使用上面定义的方法

      public class PdfParse {
    
          public static void main(final String[] args) throws Exception  {
    
    
    
            String fileName = InputFile.getFileName();
    
            FileInputStream inputstream = new FileInputStream(new File(fileName));
    
    
             ParseContext pcontext = new ParseContext();
             .........   
          } 
      }
    

    希望这会有所帮助。

    【讨论】:

    • 你从哪里得到NullPointerException
    • 知道了...立即尝试
    • fileName 返回为空(我刚刚从 PdfParse 类中打印出来)。如何纠正?
    • 那是因为fileName 被声明了两次,而外部fileName 正在进入另一个类null .. 我已经纠正了这一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    • 2010-12-27
    相关资源
    最近更新 更多