私の質問:システム標準の行セパレーターとして行セパレーターを処理するように入力ストリームを強制するにはどうすればよいですか?
ファイルを文字列に読み取り、改行はに変換されます\n
が、私のSystem.getProperty("line.separator");
は\r\n
. これを移植可能にしたいので、ファイルリーダーに改行をシステム標準の改行文字として読み取らせたい(それが何であれ)。どうすれば強制できますか?ファイルを文字列として読み取るためのJava Helper Libraryのメソッドを次に示します。
/**
* Takes the file and returns it in a string. Uses UTF-8 encoding
*
* @param fileLocation
* @return the file in String form
* @throws IOException when trying to read from the file
*/
public static String fileToString(String fileLocation) throws IOException {
InputStreamReader streamReader = new InputStreamReader(new FileInputStream(fileLocation), "UTF-8");
return readerToString(streamReader);
}
/**
* Returns all the lines in the Reader's stream as a String
*
* @param reader
* @return
* @throws IOException when trying to read from the file
*/
public static String readerToString(Reader reader) throws IOException {
StringWriter stringWriter = new StringWriter();
char[] buffer = new char[1024];
int length;
while ((length = reader.read(buffer)) > 0) {
stringWriter.write(buffer, 0, length);
}
reader.close();
stringWriter.close();
return stringWriter.toString();
}