這裏有兩個Java類:如何使用FileInputStream從另一個類訪問輸入文件?
得到輸入文件:
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異常的文件名。 我試圖使用字符串名稱,但我無法從輸入文件類使用getters獲取字符串名稱,但我失敗了。有人可以告訴我如何做到這一點?
謝謝了。
爲什麼我得到一個nullpointerexception當我使用此代碼? – smoothsipai
你在哪裏得到'NullPointerException'? – Sanjeev
明白了......現在就試試吧 – Sanjeev