2014-09-20 78 views
1

我不明白如何使用TextIO的readFile(字符串文件名) 有人可以請解釋我該如何讀取外部文件?使用TextIO讀取外部文件

public static void readFile(String fileName) { 
    if (fileName == null) // Go back to reading standard input 
    readStandardInput(); 
    else { 
    BufferedReader newin; 
    try { 
     newin = new BufferedReader(new FileReader(fileName)); 
    } 
    catch (Exception e) { 
     throw new IllegalArgumentException("Can't open file \"" + fileName + "\" for input.\n" 
         + "(Error :" + e + ")"); 
    } 
    if (! readingStandardInput) { // close current input stream 
     try { 
      in.close(); 
     } 
     catch (Exception e) { 
     } 
    } 
    emptyBuffer(); // Added November 2007 
    in = newin; 
    readingStandardInput = false; 
    inputErrorCount = 0; 
    inputFileName = fileName; 
    } 

}

+0

你什麼都不明白? – Miki 2014-09-20 23:19:47

+0

如何讀取外部文件。如何使用TextIO的這部分 – user2994326 2014-09-20 23:24:18

+0

好像你必須首先調用'readFile(「/ path/to/file)',然後使用該類的輸入方法,就像'getAnyChar()','getlnString()'或'getln()'。 – Tom 2014-09-20 23:31:38

回答

0

我不得不使用TEXTIO的作業,我就死在這了。我遇到的問題是,只要文件與我的課程位於同一文件夾中,使用Scanner類即可傳遞文件的名稱。

Scanner fileScanner = new Scanner("data.txt"); 

這工作正常。但是對於TextIO來說,這是行不通的。

TextIO.readfile("data.txt"); // can't find file 

你必須包含這樣的文件路徑;

TextIo.readfile("src/package/data.txt"); 

不知道是否有一種方法可以得到它像掃描儀類或不能正常工作,但是這是我在我的學校課程一直在做。