0

1 と 0 で構成される .txt ファイルがあります。

11111100000001010000000101110010
11111100000001100000000101110010
00000000101001100010000000100000

8 (1 と 0) を読み取って、各「バイト」をバイト配列に入れられるようにしたいと考えています。したがって、1 行は 4 バイトになります。

11111100 00000101 00000001 01110010 --> 4 bytes, line 1
11111100 00000110 00000001 01110010 --> 8 bytes, line 2
00000000 10100110 00100000 00100000 --> total 12 bytes, line 3
                ...
and so on.

データをバイナリファイルに保存する必要があると思いますが、これを行う方法がわかりません。どんな助けでも大歓迎です。

編集1:

8 つの 1 と 0 (11111100、00000101) をバイトに入れ、バイト配列に格納して、11111100 が配列の最初のバイトになり、00000101 が 2 番目のバイトになるようにします。これがより明確になることを願っています。

編集2:

fileopen = new JFileChooser(System.getProperty("user.dir") + "/Example programs"); // open file from current directory
        filter = new FileNameExtensionFilter(".txt", "txt");
        fileopen.addChoosableFileFilter(filter);

        if (fileopen.showOpenDialog(null)== JFileChooser.APPROVE_OPTION) 
        {
            try
            {
                file = fileopen.getSelectedFile();

                //create FileInputStream object
                FileInputStream fin = new FileInputStream(file);

                byte[] fileContent = new byte[(int)file.length()];

                fin.read(fileContent);

                for(int i = 0; i < fileContent.length; i++)
                {
                    System.out.println("bit " + i + "= " + fileContent[i]);
                }

                //create string from byte array
                String strFileContent = new String(fileContent);
                System.out.println("File content : ");
                System.out.println(strFileContent);                      

            }
            catch(FileNotFoundException e){}
            catch(IOException e){}
        }
4

2 に答える 2

1

コードにコメントを付けて、1 つの方法を次に示します。

import java.lang.*;
import java.io.*;
import java.util.*;

public class Mkt {
  public static void main(String[] args) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader("in.txt"));
    List<Byte> bytesList = new ArrayList<Byte>();

    // Read line by line
    for(String line = br.readLine(); line != null; line = br.readLine()) {
      // 4 byte representations per line
      for(int i = 0; i < 4; i++) {
        // Get each of the 4 bytes (i.e. 8 characters representing the byte)
        String part = line.substring(i * 8, (i + 1) * 8);
        // Parse that into the binary representation
        // Integer.parseInt is used as byte in Java is signed (-128 to 127)
        byte currByte = (byte)Integer.parseInt(part, 2);
        bytesList.add(currByte);
      }
    }

    Byte[] byteArray = bytesList.toArray(new Byte[]{});

    // Just print for test
    for(byte currByte: byteArray) {
      System.out.println(currByte);
    }
  }
}

入力は という名前のファイルから読み取られますin.txt。実行例は次のとおりです。

$ javac Mkt.java && java Mkt
-4
5
1
114
-4
6
1
114
0
-90
32
32

これが開始に役立つことを願っています。必要に応じて微調整できます。

于 2012-12-08T16:46:14.417 に答える
0

BufferedReader を使用して txt ファイルを読み込みます。

BufferedReader in = new BufferedReader(...);
ArrayList<byte> bytes = new ArrayList<byte>();
ArrayList<char> buffer = new ArrayList<char>();
int c = 0;
while((c = in.read()) >= 0) {
    if(c == '1' || c == '0') buffer.add((char)c);
    if(buffer.size() == 8) {
        bytes.add(convertToByte(buffer));
        buffer.clear();
    }
}
于 2012-12-08T16:27:28.400 に答える