From GWT I read a text file “myFile.txt” as per below. The issue is that I get different results in the “input” string depending on the server:
- If I run in from Eclipse Indigo local server (debugging), “input” includes at the end characters “\r” and “\n”.
- If I run it from Google App Engine, “input” includes at the end character “\n” only, so one character less in input.length.
Why does this happen, and how can I have the same behaviour?
Thanks
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();
}
Because line separators are different on different OSes. This is what
System.getProperty("line.separator")does.On Windows it’s
\r\n\(two chars), while on Linux it’s\n(one char). See here..