【发布时间】:2015-12-11 19:17:08
【问题描述】:
我正在尝试使用 useDelimiters() 按某个分隔符而不是每个新行来拆分文本文件。例如,在下面的代码中,我有这个文本文件输入,我想在句号处将其分成两个:
hellothere
stop.hereok
asdf
我的代码:
public static void main(String[] args) throws FileNotFoundException {
File text = new File("test.txt");
Scanner filein = new Scanner(text).useDelimiter("\\.");
System.out.println(filein.next());
System.out.println(filein.next());
}
我的输出:
hellothere
stop
hereok
asdf
预期输出:
hellotherestop
hereokasdf
有人知道这个问题吗?
【问题讨论】:
-
你试过
\n作为你的分隔符 -
扫描仪按预期工作。您可以像这样从标记中删除换行符:
System.out.println(filein.next().replaceAll("\\r|\\n", ""));. -
谢谢,我在继续我的代码时也会考虑到这一点。
标签: java regex java.util.scanner