0

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

例如我想要这个输出;

输出


RFID : 25 BLUID : 562 WifiID : 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!");

请帮我 ;

非常感谢 ...

4

5 回答 5

8

这是一个使用示例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);
    }
}
射频识别:25
蓝色:562
WifiID:2610
射频识别:33
计数:{RFID=2, BLUID=1, WifiID=1}
1103000
射频识别:25
蓝色:562
WifiID:2610
射频识别:33
计数:{RFID=2, BLUID=1, WifiID=1}
22772000
于 2010-01-17T18:52:49.313 回答
3

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

也许是这样的(我没有测试过):

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 对象,因为该方法对于您想要做的事情更灵活。

于 2010-01-17T09:08:11.483 回答
2

准备运行:

public class ScannerTest {

    private static void readFile(String fileName) {

        try {
            HashMap<String, Integer> map = new HashMap<String, Integer>();
            File file = new File(fileName);

            Scanner scanner = new Scanner(file).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);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        readFile("test.txt");
    }
}
于 2010-01-17T09:12:15.310 回答
1

你的第一行有问题。

  1. 您需要在字符串文字中转义反斜杠("i:\\1.txt"not "i:\1.txt"
  2. Scanner从文件中读取的构造函数接受一个File参数(或一个InputStream参数)。接受参数的构造String函数正在读取该实际字符串。请参阅javadoc

尝试

Scanner scanner = new Scanner(new File("i:\\1.txt"));
于 2010-01-17T08:51:07.703 回答
0

一些起始代码:

String filename = "your_text_file";
Scanner sc = new Scanner(filename);

// use the scanner to iterate through every line in the file:
try{
while(sc.hasNextLine()){
    String line = sc.nextLine();
    // split the line up into space-separated tokens:
    String[] tokens = line.split();
    // look through the tokens to find what you are looking for:
    for(int i = 0; i<tokens.length; i++){
        if(tokens[i].equals("search_word){
             // Do stuff
        }
    }
}
} // end try
catch(Exception e){}
于 2010-01-17T08:57:21.857 回答