简体   繁体   中英

Java read into byte array

I have a .txt file consisting of 1's and 0's like so;

11111100000001010000000101110010
11111100000001100000000101110010
00000000101001100010000000100000

I would like to be able to read 8 (1's and 0's) and put each 'byte' into a byte array. So a line would be 4 bytes;

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.

I believe I need to store the data in a binary file but I'm not sure how to do this. Any help is greatly appreciated.

Edit 1:

I would like to put 8 1's and 0's (11111100, 00000101) into a byte and store in a byte array so 11111100 would be the first byte in the array, 00000101 the second and so on. I hope this is clearer.

Edit 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){}
        }

Here's one way, with comments in the code:

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);
    }
  }
}

Input is read from file named in.txt . Here's a sample run:

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

Hope this helps to get you started, you can tweak to your needs.

Use BufferedReader to read in the txt file.

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM