2012-08-12 40 views
0

從GWT中讀取文本文件「myFile.txt」,如下所示。問題是,我得到在「輸入」字符串不同的結果,這取決於在服務器上:從Eclipse本地服務器和Google App Engine讀取文本文件的不同結果

  • 如果我從Eclipse的靛藍本地服務器(調試)運行「輸入」包括在末端字符「\ r」和「\ n」。
  • 如果我從Google App Engine運行它,「輸入」僅包含結尾字符「\ n」,因此在input.length中少一個字符。

爲什麼會發生這種情況,我怎麼能有相同的行爲?

由於

String input=readFromFile("myFile.txt"); 

public String readFromFile(String fileName) { 
    File file = new File(fileName); 
    StringBuffer contents = new StringBuffer(); 
    BufferedReader reader = null; 
    try { 
    reader = new BufferedReader(new FileReader(file)); 
    String text = null; 
    while ((text = reader.readLine()) != null) { 
    contents.append(text).append(System.getProperty("line.separator")); 
    } 
    } catch (FileNotFoundException e) { 
    e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } finally { 
    try { 
     if (reader != null) { 
     reader.close(); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    } 
    contents.deleteCharAt(contents.length()-2); // Remove to make it work in GAE 
    contents.deleteCharAt(contents.length()-1); 
    return contents.toString(); 
} 

回答

相關問題