任务:从输入文件中读取一行。如果该行的第一个单词是PRINT,则打印该行其余部分的内容。
代码:
else if(Data.compareTo("PRINT") == 0){
while(inFile.hasNext()){
Data = inFile.next();
System.out.print( Data + " ");
}
}
问题:如何对扫描仪进行编码,以便扫描仪一次只读取一行信息?
答案 0 :(得分:0)
public static void ReadAndProcessPrint(File fileToRead) throws FileNotFoundException {
java.util.Scanner scanner = new Scanner(fileToRead);
while(scanner.hasNextLine()){
String line = scanner.nextLine();
if(line.startsWith("PRINT")){
String restOfLine = line.substring(5);
System.out.println(restOfLine);
}else{
//do other things
}
}
}
答案 1 :(得分:-1)
提示:http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html
创建一个InputStreamReader并使用它创建一个BufferedReader,使用readLine方法。