2014-09-20 165 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"); 

不知道是否有一种方法可以得到它像扫描仪类或不能正常工作,但是这是我在我的学校课程一直在做。