gpt4 book ai didi

java - 我该如何用扫描仪(java)处理它?

转载 作者:行者123 更新时间:2023-12-01 06:33:57 24 4
gpt4 key购买 nike

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

例如我想要这个输出;

输出

<小时/>

RFID:25蓝色:562无线网络ID:2610射频识别: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!");
<小时/>

请帮助我;

非常感谢...

最佳答案

这是一个使用 StreamTokenizer 的示例:

import java.io.IOException;
import java.io.StreamTokenizer;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Scanner;

public class ScannerTest {

private static final String s = ""
+ "AAAAAAAAAAAA;RFID=25;\n"
+ "BBBB;BBBBBBBB;BBBBBBBBBB;\n"
+ "CCCCC;fffdsfdsfdfsd;BLUID=562;dfsdfsf;\n"
+ "fgfdgdf;terter;fdgfdgtryt;\n"
+ "trtretrre;WifiID=2610;trterytuytutyu;\n"
+ "zxzxzxzxz;popopopwwepp;RFID:33;aasasds…\n"
+ "gfdgfgfd;gfdgfdgfd;fdgfgfgfd;\n";

public static void main(String[] args) {
long start = System.nanoTime();
tokenize(s);
System.out.println(System.nanoTime() - start);
start = System.nanoTime();
scan(s);
System.out.println(System.nanoTime() - start);
}

private static void tokenize(String s) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
StreamTokenizer tokens = new StreamTokenizer(new StringReader(s));
tokens.whitespaceChars(';', ';');
try {
int token;
String id;
do {
id = tokens.sval;
token = tokens.nextToken();
if (token == '=' || token == ':') {
token = tokens.nextToken();
Integer count = map.get(id);
map.put(id, count == null ? 1 : count + 1);
System.out.println(id + ":" + (int) tokens.nval);
}
} while (token != StreamTokenizer.TT_EOF);
System.out.println("Counts:" + map);
} catch (IOException e) {
e.printStackTrace();
}
}

private static void scan(String s) {
HashMap<String, Integer> map = new HashMap<String, Integer>();
Scanner scanner = new Scanner(s).useDelimiter(";");
while (scanner.hasNext()) {
String token = scanner.next();
String[] split = token.split(":");
if (split.length == 2) {
Integer count = map.get(split[0]);
map.put(split[0], count == null ? 1 : count + 1);
System.out.println(split[0] + ":" + split[1]);
} else {
split = token.split("=");
if (split.length == 2) {
Integer count = map.get(split[0]);
map.put(split[0], count == null ? 1 : count + 1);
System.out.println(split[0] + ":" + split[1]);
}
}
}
scanner.close();
System.out.println("Counts:" + map);
}
}
RFID:25BLUID:562WifiID:2610RFID:33Counts:{RFID=2, BLUID=1, WifiID=1}1103000RFID:25BLUID:562WifiID:2610RFID:33Counts:{RFID=2, BLUID=1, WifiID=1}22772000

关于java - 我该如何用扫描仪(java)处理它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2080403/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com