【发布时间】:2012-03-01 11:04:57
【问题描述】:
我的程序中有一个扫描仪,可以读取部分文件并将其格式化为 HTML。当我阅读我的文件时,我需要知道如何让扫描仪知道它位于行尾并开始写入下一行。
这是我的代码的相关部分,如果我遗漏了任何内容,请告诉我:
//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(),但我仍然不知道如何确定我何时处于行尾。
【问题讨论】:
-
当你结束时,sc.hasNext() 将是假的,不是吗?
-
澄清一下,你确定你不想知道如何让
FileWriter在另一行开始写作吗?从您的帖子中可以采用这种方式,因为 Scanner 具有您需要的功能。
标签: java java.util.scanner delimiter