以下代码:
String line = new String("Hello World!");
Scanner scan = new Scanner(line);
我试图将scan
的来源从line
更改为另一个字符串。有没有办法为Scanner对象指定新的源代码而不必关闭它并使用新的源创建新对象?
编辑:
在使用此代码的程序中,我使用缓冲流逐行读取文本,使用Scanner对象挑选单个单词,使用shiftWord
方法转换每个单词,并使用另一个缓冲区将其写入单独的文件。
try(InputStream in = new FileInputStream(this.fileRead)) {
Reader reader = new InputStreamReader(in, "UTF-8");
BufferedReader buffIn = new BufferedReader(reader);
try(OutputStream out = new FileOutputStream(this.fileWrite)) {
Writer writer = new OutputStreamWriter(out, "UTF-8");
BufferedWriter buffOut = new BufferedWriter(writer);
String line = new String();
Scanner scan = new Scanner(line);
line = buffIn.readLine();
String token = new String();
String sentence;
String message = new String();
while(line != null) {
line += "/stop/";
sentence = "";
do {
token = scan.next();
sentence += (shiftWord(token) + " ");
} while(!token.endsWith("/stop/"));
message += sentence + "\n";
line = buffIn.readLine();
}
buffOut.write(message);
scan.close();
buffOut.close();
} catch (IOException x) {
System.out.println("IOException: Write Error!");
}
buffIn.close();
} catch (IOException x) {
System.out.println("IOException: Read Error!");
}
代码编译正常,但运行它会导致此行NoSuchElementException
:
token = scan.next();
我做了一些调试,我发现问题在于:
line += "stop";
因为在我声明扫描程序后,此行出现在while循环内。 要解决这个问题,我必须在此行之后声明Scanner,它位于while循环内部。 然而,我不喜欢在while循环中声明Scanner的想法,因为这将在while循环的每次迭代中创建一个新的Scanner对象。如果可能的话,我想:
基本上,重用 Scanner对象。
如果你能帮助我,我们将不胜感激。
编辑:我还想传递whitspace,其格式与原始文本文件中所用的格式相同。
答案 0 :(得分:1)
您无法更改扫描仪的基础输入流 - 它设计为从单个输入读取,直到它为空。你可以将扫描器包裹在一个缠绕在ByteArrayInputStreams的LinkedList周围的SequentialInputStream上,然后可以在运行时进行扩展,但是我很难想象这会是一个好主意的情况。
在您的情况下:为什么不将扫描仪直接包裹在buffIn周围?将扫描仪配置为在使用它们时不忽略换行符(useDelimiter("[\\s&[^\\n\\r]]*")
)。这样,您只需要创建一个扫描仪。
编辑:此示例使用单词边界作为分隔符:
public class ScannerExample {
public static void main(String[] args) {
Scanner s = new Scanner("this is a \n\tTest");
s.useDelimiter("\\b");
while (s.hasNext()) {
String token = s.next();
System.out.println("\"" + StringEscapeUtils.escapeJava(token)
+ "\"\tapply shiftToken = " + !token.matches("\\A\\s+\\z"));
}
}
}
输出:
"this" apply shiftToken = true
" " apply shiftToken = false
"is" apply shiftToken = true
" " apply shiftToken = false
"a" apply shiftToken = true
" \n\t" apply shiftToken = false
"Test" apply shiftToken = true