我如何用扫描仪(java)处理它?
我有一个关于扫描仪的问题;我在一家小公司工作;我们有一个软件;它生成一个大文本文件;我们必须从中得到一些有用的信息;我想用java编写一个简单的应用程序以节省时间;你能指导一下吗?
例如我想要这个输出;
输出
RFID:25 蓝色:562 无线网络ID:2610 RFID:33
RFID 计数:2
例如;这是我的文本文件,因为我们的软件生成的每个文件都有 14000 行:)
--------------------------
AAAAAAAAAAAA;RFID=25;
BBBB;BBBBBBBB;BBBBBBBBBB;
CCCCC;fffdsfdsfdfsd;BLUID=562;dfsdfsf;
fgfdgdf;terter;fdgfdgtryt;
trtretrre;WifiID=2610;trterytuytutyu;
zxzxzxzxz;popopopwwepp;RFID:33;aasasds…
gfdgfgfd;gfdgfdgfd;fdgfgfgfd;
我用这个源代码测试它,但我无法处理它;
Scanner scanner = new Scanner("i:\1.txt");
scanner.findInLine("RFID=");
if (scanner.hasNext())
System.out.println(scanner.next());
else
System.out.println("Error!");
请帮我 ;
多谢 ...
I have a question about scanner please;I working at a small company; we have a software; it generate a big text file; and we must get some useful information from it ; i want write a simple application with java for saving time; could you please guide me ?
for example i want this output ;
Output
RFID : 25
BLUID : 562
WifiID : 2610
RFID : 33
RFID Count : 2
and for example ;this is my Text file, because each generated file with our software has 14000 lines :)
--------------------------
AAAAAAAAAAAA;RFID=25;
BBBB;BBBBBBBB;BBBBBBBBBB;
CCCCC;fffdsfdsfdfsd;BLUID=562;dfsdfsf;
fgfdgdf;terter;fdgfdgtryt;
trtretrre;WifiID=2610;trterytuytutyu;
zxzxzxzxz;popopopwwepp;RFID:33;aasasds…
gfdgfgfd;gfdgfdgfd;fdgfgfgfd;
I test it with this source code but i can't handle it;
Scanner scanner = new Scanner("i:\1.txt");
scanner.findInLine("RFID=");
if (scanner.hasNext())
System.out.println(scanner.next());
else
System.out.println("Error!");
please help me ;
Thanks a lot ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是使用
StreamTokenizer
< 的示例/a>:Here's an example using
StreamTokenizer
:那么您建议的来源不会做您想要的事情。扫描仪使用分隔符分隔输入。默认分隔符是空格(空格、制表符或换行符)。 Scanner.hasNext() 只是告诉您是否有新的空格分隔标记。 Scanner.next() 只是返回该令牌。请注意,这些都不受 Scanner.findInLine(pattern) 的影响,因为它所做的只是在当前行中搜索提供的模式。
也许是这样的(我没有测试过):
我建议您忘记使用扫描仪,而只使用 BufferedReader 和几个 Pattern 对象,因为该方法对于您想做的事情更加灵活。
Well your suggested source would not do what you want. Scanner breaks up input using a delimiter. The default delimiter is whitespace (spaces, tabs or newlines). Scanner.hasNext() simply tells you if there is a new whitespace delimted token. Scanner.next() simply returns that token. Note that none of these are effected by Scanner.findInLine(pattern) as all it does is search the current line for the provided pattern.
Maybe something like this (I have not tested this):
I would recommend you forget about using scanner and just use a BufferedReader and a couple of Pattern objects as that method is more flexible for what you want to do.
准备运行:
ready to run:
你的第一行有问题。
"i:\\1.txt"
而不是"i:\1.txt"
File
参数(或InputStream
参数)。采用String
参数的构造函数正在读取该实际字符串。请参阅 javadoc。尝试
Your first line is problematic.
"i:\\1.txt"
not"i:\1.txt"
)Scanner
constructor for reading from a file takes aFile
argument (or anInputStream
argument). The constructor which takes aString
argument is reading from that actual string. See the javadoc.Try
一些起始代码:
Some starting code: