Java 如何使用FileInputStream从另一个类访问输入文件?

Java 如何使用FileInputStream从另一个类访问输入文件?,java,input,file-io,inputstream,user-input,Java,Input,File Io,Inputstream,User Input,以下是两个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){

以下是两个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类获取字符串名称,但失败。有人能告诉我怎么做吗


非常感谢

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

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

您可以在
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;
    }
}
  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();
         .........   
      } 
  }
然后您可以在
PdfParse
类的
main
方法中使用上述定义的方法

 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;
    }
}
  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
?得到了。。。立即试用返回的文件名为null(我刚从PdfParse类打印了它)。如何更正它?这是因为
fileName
声明了两次,而外部
fileName
正在进入另一个
null
的类。。我已经改正了谢谢Jorn!但另一个答案更简单。:)