Java 如何使用扫描仪确定线的端点?
我的程序中有一个扫描器,可以读取部分文件并将其格式化为HTML。当我读取文件时,我需要知道如何让扫描仪知道它在一行的末尾,并开始写入下一行 这是我代码的相关部分,如果我遗漏了什么,请告诉我:Java 如何使用扫描仪确定线的端点?,java,java.util.scanner,delimiter,Java,Java.util.scanner,Delimiter,我的程序中有一个扫描器,可以读取部分文件并将其格式化为HTML。当我读取文件时,我需要知道如何让扫描仪知道它在一行的末尾,并开始写入下一行 这是我代码的相关部分,如果我遗漏了什么,请告诉我: //scanner object to read the input file Scanner sc = new Scanner(file); //filewriter object for writing to the output file FileWriter fWrite = new FileWr
//scanner object to read the input file
Scanner sc = new Scanner(file);
//filewriter object for writing to the output file
FileWriter fWrite = new FileWriter(outFile);
//Reads in the input file 1 word at a time and decides how to
////add it to the output file
while (sc.hasNext() == true)
{
String tempString = sc.next();
if (colorMap.containsKey(tempString) == true)
{
String word = tempString;
String color = colorMap.get(word);
String codeOut = colorize(word, color);
fWrite.write(codeOut + " ");
}
else
{
fWrite.write(tempString + " ");
}
}
//closes the files
reader.close();
fWrite.close();
sc.close();
我发现了关于
sc.nextLine()
,但我仍然不知道如何确定我何时到达终点。哇,我已经使用java 10年了,从来没有听说过scanner!
默认情况下,它似乎使用空白分隔符,因此您无法判断何时出现行尾
看起来您可以更改扫描仪的分隔符-请参见以下示例:
行通常由
\n
或\r
分隔,因此如果需要检查,可以尝试这样做,尽管我不确定您为什么要这样做,因为您已经在使用nextLine()
读取整行
如果您担心
hasNextLine()
不适用于您的特定情况(不确定为什么不适用),则有Scanner.hasNextLine()
。您可以使用hasNextLine方法逐行迭代文件,而不是逐字迭代,然后按空格分割该行并对该字执行操作
下面是使用hasNextLine和split的相同代码
//scanner object to read the input file
Scanner sc = new Scanner(file);
//filewriter object for writing to the output file
FileWriter fWrite = new FileWriter(outFile);
//get the line separator for the current platform
String newLine = System.getProperty("line.separator");
//Reads in the input file 1 word at a time and decides how to
////add it to the output file
while (sc.hasNextLine())
{
// split the line by whitespaces [ \t\n\x0B\f\r]
String[] words = sc.nextLine().split("\\s");
for(String word : words)
{
if (colorMap.containsKey(word))
{
String color = colorMap.get(word);
String codeOut = colorize(word, color);
fWrite.write(codeOut + " ");
}
else
{
fWrite.write(word + " ");
}
}
fWrite.write(newLine);
}
//closes the files
reader.close();
fWrite.close();
sc.close();
如果只想使用Scanner,则需要创建一个临时字符串,将其实例化为数据网格的nextLine()(因此它只返回跳过的行),并创建一个扫描临时字符串的新Scanner对象。这样,您只使用该行,而hasNext()不会返回假阳性(它不是真的假阳性,因为这是它的本意,但在您的情况下,它在技术上是)。您只需保持nextLine()运行第一个扫描程序,并更改临时字符串和第二个扫描程序以扫描每一行,等等。当您在sc结尾时。HASTNEXT()将为false,不是吗?请澄清,您确定不想知道如何让
FileWriter
从另一行开始写入吗?从您的帖子中可以看出,由于扫描仪具有您所需的功能,所以可以采用这种方式。这可能与“您需要创建一个临时字符串并将其实例化为nextLine()”的意思重复?
//scanner object to read the input file
Scanner sc = new Scanner(file);
//filewriter object for writing to the output file
FileWriter fWrite = new FileWriter(outFile);
//get the line separator for the current platform
String newLine = System.getProperty("line.separator");
//Reads in the input file 1 word at a time and decides how to
////add it to the output file
while (sc.hasNextLine())
{
// split the line by whitespaces [ \t\n\x0B\f\r]
String[] words = sc.nextLine().split("\\s");
for(String word : words)
{
if (colorMap.containsKey(word))
{
String color = colorMap.get(word);
String codeOut = colorize(word, color);
fWrite.write(codeOut + " ");
}
else
{
fWrite.write(word + " ");
}
}
fWrite.write(newLine);
}
//closes the files
reader.close();
fWrite.close();
sc.close();