如何读取二进制文件并将它们排序到java中的数组中?快速

时间:2016-08-24 17:33:38

标签: java

我从ARINC 429总线通过wireshark获取了大量数据,这只是十六进制的原始数据。当我尝试在记事本中打开它时,它将数据读取为ascii。我正在尝试解析数据并将其分成单词。查看wireshark中的数据,我知道单词以十六进制0x10 0x42开头,以0x10 0x03结尾。我无法让java读取文件并使用这些分隔符将其分成单词。我将数据作为C数组拉出并将其放入here。以下是我要解析的数据示例。 http://www.filedropper.com/datasample

我对java有一些经验,但还是比较新的。任何有关如何让java读取而不丢失数据的帮助将不胜感激。

这是我到目前为止所尝试的:

public class FileData {
    public static void main(String[] args) {

    String file_name = "C:/Users/dhamilton/Documents/20160816 test flight/this one.txt";

    String[] delimiter = new String[256];
    String[] labels = new String[256];

    for (int j = 0; j < 256; j++) {
        String hex = Integer.toHexString(j);
        delimiter[j] = "0x10, 0x41, 0x" + hex;

        String oct = Integer.toOctalString(j);

        labels[j] = oct;
    }

    try {
        ReadFile file = new ReadFile(file_name);
        String[] aryLines = file.OpenFile();

        //String sure = Integer.toBinaryString(i); //FIX THIS
            //System.out.println(sure);


        for (int i=0; i<aryLines.length; i++) {
            //System.out.println(aryLines[i].length() + "\t" + aryLines[i]);
            for (int p=0; p<255; p++) {
                if (aryLines[i].contains(delimiter[p])) {
                    file.addOne(p);
                }
            }      
        }

        for (int q=0; q<256; q++) {
            if (file.getCounter(q) != 0) {
                System.out.println("Label " + labels[q] + ":\t" + file.getCounter(q));
            }
        }
    }
    catch (IOException e) {
        System.out.println(e.getMessage());
    }

}
}

以及:

public class ReadFile {
public String path;
public int[] counter = new int[256];


public ReadFile(String file_path) {
    path = file_path;
}

int readLines() throws IOException {
    FileReader file_to_read = new FileReader(path);
    BufferedReader bf = new BufferedReader(file_to_read);

    String aLine;
    int numberOfLines = 0;

    while (( aLine = bf.readLine()) != null) {
        numberOfLines++;
    }
    bf.close();

    return numberOfLines;
}

public String[] OpenFile() throws IOException {

    FileReader fr = new FileReader(path);
    BufferedReader textReader = new BufferedReader (fr);

    int numberOfLines = readLines();

    String[] textData = new String[numberOfLines];

    for (int i = 0; i < numberOfLines; i++) {
        textData[i] = textReader.readLine();
    }

    textReader.close();

    return textData;

}

public int getCounter(int thatOne) {
    return counter[thatOne];
}

public void addOne(int k) { 
    counter[k]++;
}
}

0 个答案:

没有答案