换行符如何使用java检查文件中存储的文本是否包含换行符(Enter)?
我试图读取文本文件中存储的数据,并检查文件的数据是否包含新行字符。 例如:如果文件包含数据:
This is first line.
This is second line.
然后我的程序应该说第一行包含新行字符
我试着用
File file = new File(System.getProperty("user.dir") + File.separator + "test.txt");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
while ((line = bufferedReader.readLine()) != null)
{
char[] arr = line.toCharArray();
for(char test: arr)
if(String.valueOf(test) == System.getProperty("line.separator"))
System.out.println("Contains new line");
}
我也试过这个代码
while ((line = bufferedReader.readLine()) != null)
{
if (line.contains(System.getProperty("line.separator")))
System.out.println("Contains new line");
}
但没有用
我的文件不包含新行字符。在notepad++中,如果搜索\n或\r\n,它会在文件中显示新行字符。但有些人认为我的代码无法识别它
# 1 楼答案
正如你可以从
BufferedReader#readLine
JavaDoc中读到的那样这就是为什么你的行没有行尾