我用 java 制作了一个简单的解析器,它一次读取一个字符并构造单词。
我尝试在 Linux 下运行它,我发现查找 '\n'
不起作用。尽管如果我将字符与值 10
进行比较,它会按预期工作。根据 ASCII 表,值 10 是 LF(换行)。我在某处读到(我不记得在哪里),Java 应该只能通过查找 '\n'
来找到换行符。
我正在使用 BufferedReader
和 read
方法来读取字符。
编辑
readLine
不能使用,因为它会产生其他问题
看起来这个问题是在我在linux下使用以mac/windows文件结尾的文件时出现的。
最佳答案
使用readLine()
逐行读取文本
示例
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
关于java - 行尾困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8700575/