我在我的程序中有一個掃描儀讀取部分文件和格式爲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();,但我仍然不知道如何確定我什麼時候在一行的結尾。
提示:使您的文章標題成爲特定問題。 – nes1983 2012-02-08 23:18:16
當你在結束sc.hasNext()將是假的,不是嗎? – kosa 2012-02-08 23:18:55
只是要清楚,你確定你不想知道如何讓'FileWriter'開始寫在另一行?從您的帖子可以採取這種方式,因爲掃描儀具有您需要的功能。 – 2012-02-08 23:37:18