如何用Java中的Scanner类解决行尾读取问题
我不是一位经验丰富的 Java 程序员,我正在尝试将一些文本写入文件,然后使用 Scanner 读取它。我知道有很多方法可以做到这一点,但我想将记录写入带有分隔符的文件,然后读取这些片段。
问题这么小。当我查看输出时,没有看到一些打印(如下所示)。我的意思是输出中仅写有“扫描仪”的粗线。如果有人能回答为什么没有看到“String:”,我将不胜感激。 (请回答我的问题)
我不明白这是一个简单的打印问题还是“\r\n”的行结束问题。
这是代码:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Tmp {
public static void main(String args[]) throws IOException {
int i;
boolean b;
String str;
FileWriter fout = new FileWriter("test.txt");
fout.write("Testing|10|true|two|false\r\n");
fout.write("Scanner|12|one|true|");
fout.close();
FileReader fin = new FileReader("Test.txt");
Scanner src = new Scanner(fin).useDelimiter("[|\\*]");
while (src.hasNext()) {
if (src.hasNextInt()) {
i = src.nextInt();
System.out.println("int: " + i);
} else if (src.hasNextBoolean()) {
b = src.nextBoolean();
System.out.println("boolean: " + b);
} else {
str = src.next();
System.out.println("String: " + str);
}
}
fin.close();
}
}
这是输出:
String: Testing
int: 10
boolean: true
String: two
String: false
Scanner
int: 12
String: one
boolean: true
I am not an experienced Java programmer and i'm trying to write some text to a file and then read it with Scanner. I know there are lots of ways of doing this, but i want to write records to file with delimiters, then read the pieces.
The problem is so small. When I look the output some printing isn't seen(shown in below). I mean the bold line in the Output that is only written "Scanner". I would be appreciated if anyone can answer why "String: " isn't seen there. (Please answer just what i ask)
I couldn't understand if it is a simple printing problem or a line end problem with "\r\n".
Here is the code:
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Tmp {
public static void main(String args[]) throws IOException {
int i;
boolean b;
String str;
FileWriter fout = new FileWriter("test.txt");
fout.write("Testing|10|true|two|false\r\n");
fout.write("Scanner|12|one|true|");
fout.close();
FileReader fin = new FileReader("Test.txt");
Scanner src = new Scanner(fin).useDelimiter("[|\\*]");
while (src.hasNext()) {
if (src.hasNextInt()) {
i = src.nextInt();
System.out.println("int: " + i);
} else if (src.hasNextBoolean()) {
b = src.nextBoolean();
System.out.println("boolean: " + b);
} else {
str = src.next();
System.out.println("String: " + str);
}
}
fin.close();
}
}
Here is the output:
String: Testing
int: 10
boolean: true
String: two
String: false
Scanner
int: 12
String: one
boolean: true
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有正确设置分隔符;
[|\\*]
是由 2 个字符|
、*
组成的字符类。您似乎发现
"[|\\n]"
“有效”,但这实际上在某些标记的末尾留下了尾随\r
。巧合的是,您应该查找
PrintWriter
;它具有使用系统属性
line.separator
的println
方法。它基本上就是System.out
的本质。You're not setting your delimiter right;
[|\\*]
is a character class consisting of 2 characters,|
,*
.You seemed to have found that
"[|\\n]"
"works", but this actually leaves a trailing\r
at the end of some tokens.Coincidentally, you should look up
PrintWriter
; it hasprintln
methods that uses the system propertyline.separator
. It's basically whatSystem.out
is-a.问题是您正在写出字符串“String:”,然后写出控制字符 \r 或回车符,然后写出内容。
以下版本应该更适合您:
要真正了解我正在谈论的 \r,您应该更改原始程序,以便打印代码如下所示:
您应该看到输出:
The problem is that you are writing out the String "String: " and then writing out control character \r, or carriage return, and then writing out the contents.
The following version should work a bit better for you:
To really see what I am talking about with the \r, you should change your original program so the print code looks like this:
You should see the output: