0

I'm trying to use both tabs and newlines as delimiters to read from a .txt file. What I have at the moment is:

Scanner fileScanner = new Scanner(new FileReader("propertys.txt"));

fileScanner.useDelimiter("[\\t\\n]");

I've tried:

fileScanner.useDelimiter("\\t|\\n");

and

fileScanner.useDelimiter("[\\t|\\n]");

I've got no idea what's going wrong, I've searched around a lot and it looks like one of those should be working. Clearly I'm doing something wrong.

4

2 回答 2

1

fileScanner.useDelimiter("\t|\n"); should work.

If you have two slashes "\n" the first acts as an escape and it won't work right.

于 2014-09-10T22:15:03.810 回答
0

对于在 useDelimiter 方法中用作参数的正则表达式,您应该使用换行符\n代替而不使用\\n制表符\t代替\\t。来自 Java 模式类:http ://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html 。

其中的一部分,我认为你应该定义你的正则表达式,例如,这个:

fileScanner.useDelimiter("\\s*[\t\n]\\s*");

限制\\s换行符或制表符之间的字符串 ( )。

于 2013-05-27T21:11:14.983 回答