[英]“properly” Manipulate “cursor” when reading an input file when using scanners
这是从.txt输入文件的一行文本中提取字符和字符串的“正确方法”吗?
Scanner scan, readLine;
System.out.print("Enter the name of your transaction file please (include .txt extension): ");
transFilename = scan.nextLine();
scan = new Scanner(new FileReader(transFilename));
while (scan.hasNext())
{
// read a whole line, then use scanner to parse
readLine = new Scanner(scan.nextLine());
code = readLine.next().toUpperCase().charAt(0);
switch (code) {
case 'A':
lineinput = readLine.next();
filename = lineinput;
System.out.println(filename);
Scanner input = new Scanner( new FileReader(filename));
break;
}
示例输入文本文件读取为:
A blahblah.txt
B
A gahgah.txt
当此行代码运行时:lineinput = readLine.next(); 在while循环的迭代过程中,光标是否返回到行的开头? 我只想知道扫描仪“光标”如何在每一行中移动。
示例:(^表示光标位置):
A blahblah.txt
^
在循环的每次遍历中,您都从文件扫描器获取新行,然后创建仅在该行上进行迭代的新Scanner实例。 在lineinput = readLine.next();
,光标在'A'
行上的文件名之后; 但是,外部扫描仪的光标在下一行的开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.