简体   繁体   English

如何存储字节并将其读回数组

[英]How to store bytes and read them back to an array

I am trying to store a list of numbers(bytes) into a file so that I can retrieve them into a byte[]. 我试图将一个数字(字节)列表存储到一个文件中,以便可以将它们检索到一个字节[]中。

59 20 60 21 61 22 62 23 63 24 64 25 65 26 66 27 67 28 68 29 
67 30 66 31 65 32 64 33 63 34 62 35 61 36 60 37 59 38
66 29 65 30 64 31 63 32 62 33 61 34 60 35 59 36 58 37
65 28 64 29 63 30 62 31 61 32 60 33 59 34 58 35 57 36...

I have tried saving them into a text file but the relevant code doesn't seem to read it properly. 我尝试将它们保存到文本文件中,但相关代码似乎无法正确读取它。

    try {
        File f = new File("cube_mapping2.txt");
        array = new byte[file.size()]
        FileInputStream stream = new FileInputStream(f);
        stream.read(array);
    } catch (Exception e) {
        e.printStackTrace();
    }

Is there a proper way to save the file so that FileInputReader.read(byte[] buffer) will populate the array with my bytes? 是否有保存文件的正确方法,以便FileInputReader.read(byte[] buffer)用我的字节填充数组?

I'd be using Scanner . 我会使用Scanner Something like this: 像这样:

public static void main(String[] args) throws IOException {
    InputStream stream = new FileInputStream("cube_mapping2.txt");
    Scanner s = new Scanner(stream);
    List<Byte> bytes = new ArrayList<Byte>();
    while (s.hasNextByte()) {
        bytes.add(s.nextByte());
    }
    System.out.println(bytes);
}

I tested this on a file containing your exact input and it worked. 我在包含您确切输入的文件上对此进行了测试,并且可以正常工作。 Output was: 输出为:

[59, 20, 60, 21, 61, 22, 62, 23, 63, 24, 64, 25, 65, 26, 66, 27, 67, 28, 68, 29, 67, 30, 66, 31, 65, 32, 64, 33, 63, 34, 62, 35, 61, 36, 60, 37, 59, 38, 66, 29, 65, 30, 64, 31, 63, 32, 62, 33, 61, 34, 60, 35, 59, 36, 58, 37, 65, 28, 64, 29, 63, 30, 62, 31, 61, 32, 60, 33, 59, 34, 58, 35, 57, 36]

FileInputStream works on binary files. FileInputStream适用于二进制文件。 The code you posted would read from a binary file, but isn't quite right because stream.read(array) reads up to the length of the array; 你的代码将发布从二进制文件阅读,但并不完全正确,因为stream.read(数组)中读取数组的长度; it doesn't promise to read the whole array. 它不会保证读取整个数组。 The return value from read(array) is the number of bytes actually read. read(array)的返回值是实际读取的字节数。 To be sure of getting all the data you want you need the read() call to be in a loop. 为了确保获得所有想要的数据,您需要将read()调用置于循环中。

To answer your actual question: to write to a file in such a way that stream.read(array) will be able to read it back it, use FileOutputStream.write(array). 要回答您的实际问题:使用FileOutputStream.write(array)以stream.read(array)能够将其读回的方式写入文件,请使用FileOutputStream.write(array)。

If you're happy with a text file instead of a binary file, go with @Bohemian's answer. 如果您对文本文件而不是二进制文件感到满意,请使用@Bohemian的答案。

array = new byte[file.size()]

Is that means, there is no space left to store the separate mark for each two numbers? 这是否意味着每个数字都没有空间存储单独的标记? According to your byte array, if each one of them is only two spaces, then you can use a two spaces temp byte array to read each byte you store in your file. 根据您的字节数组,如果它们中的每个仅是两个空格,则可以使用两个空格的临时字节数组来读取存储在文件中的每个字节。 Something like 就像是

byte[] temp = new byte[2];
stream.read(temp);

Which can make sure to read the byte number one by one. 可以确保一一读取字节数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何读取整数并将其存储在Java中的数组中 - how to read integers and store them in an array in java 如何在Java中存储无符号字节的存储(数组)以对其进行位操作? - how to store store (an array of) unsigned bytes in Java to do bit manipulations on them? 如何读取具有多种数据类型的文本文件并将它们存储在数组中? - How to read a text file with multiple data types and store them in an Array? 如何从文件中读取行并将其存储到数组中 - How do I read lines from a file and store them into an array 如何读取 JSON 数组并将它们存储在 stringbuilder 中以显示它? - How to read the JSON array and store them in a stringbuilder to display it? 如何从非常重的文件中读取字节? 然后将其存储在字符串中,例如.pdf .zip .xlsx文件 - How can i read bytes from a very heavy file? then store store them in a String e.g. .pdf .zip .xlsx files 如何从文件中读取特定数量的字节到Java中的bytes数组中? - How to read a specific number of bytes from a file into a bytes array in Java? 如何在zip中写入字节数组,然后从那里读取 - How to write an array of bytes in zip and then read it from there 您如何从输入中读取一系列数字并将其存储到数组中? - How do you read a seriers of numbers from input and store them into an array? 如何从文本文件中读取单个字符并将它们存储到二维数组中? - How do I get individual characters from read in text file and store them into a 2D array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM