我如何使用scanner(java)处理它? [英] How can i handle it with scanner (java)?

查看:121
本文介绍了我如何使用scanner(java)处理它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有关于扫描仪的问题;我在一家小公司工作;我们有一个软件;它会生成一个大文本文件;我们必须从中获得一些有用的信息;我想用java编写一个简单的应用程序来节省时间;你可以指导我吗?

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 ;

输出

RFID:25
BLUID:562
WifiID:2610
RFID:33

RFID : 25 BLUID : 562 WifiID : 2610 RFID : 33

RFID数量:2

例如;这是我的文本文件,因为每个使用我们软件生成的文件都有14000行:)

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 ;

非常感谢...

推荐答案

那么你的建议来源不会做你想要的。扫描仪使用分隔符分解输入。默认分隔符是空格(空格,制表符或换行符)。 Scanner.hasNext()只是告诉您是否有新的空格分隔标记。 Scanner.next()只返回该标记。请注意,这些都不受Scanner.findInLine(模式)影响,因为它所做的就是在当前行搜索提供的模式。

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):

Scanner scanner = new Scanner("i:\\1.txt");
scanner.useDelimiter(";");
Pattern words = Pattern.compile("(RFID=|BLUID=|WifiID=)");//just separate patterns with |
while (scanner.hasNextLine()) {
  key = scanner.findInLine(words);
  while (key != null) {
    String value = scanner.next();
    if (key.equals("RFID=") {
      System.out.print("RFID:" + value);
    } //continue with else ifs for other keys
    key = scanner.findInLine(words);
  }
  scanner.nextLine();
}

我建议你忘记使用扫描仪,只使用BufferedReader和一些Pattern对象,因为这种方法对你想做的事情更灵活。

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.

这篇关于我如何使用scanner(java)处理它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆