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