我正在读取文件中的输入,其中文本由一个或多个换行符分隔.忽略我使用的空白换行 input.useDelimiter("\\n");但是由于某种原因,nextLine() - 方法读取空行而不是忽略它们.我究竟做错了什么?
编辑:假设第一行是空白换行符,第二行是字符串 "ABC"
input.useDelimiter("[\n]+");
String kjkj = input.nextLine();
System.out.println("***"+kjkj+"***");
Run Code Online (Sandbox Code Playgroud)
给出这个结果:******
而不是***ABC***
Scanner#nextLine() 不使用分隔符模式,它使用自己的内部模式来检查新行的每个实例.
要修复它,请使用Scanner#next()分隔符模式"\\n+"检查行中的多个新行.
// Change the delimiter to the newline char
// \\r used just for Windows compatibility
input.useDelimiter("[\\r\\n]+");
// Get the next non-blank line
String nextLineThatHasSomething = input.next();
Run Code Online (Sandbox Code Playgroud)