简体   繁体   English

从文件读取字节或向文件写入字节

[英]Reading and writing bytes from/to a file

I'm trying to write an array of bytes to a file and read them afterwards. 我正在尝试将字节数组写入文件并随后读取它们。 It's important that the the array of bytes that I write is the same as the one that I read. 重要的是,我写入的字节数组必须与我读取的字节数组相同。 I tried some methods that were suggested here ( File to byte[] in Java ). 我尝试了一些建议的方法( 在Java中是File to byte [] )。 However when I apply them I end up reading a different array that I initially wrote. 但是,当我应用它们时,我最终会读取最初编写的另一个数组。

Here's what i tried: 这是我尝试过的:

import java.io.File;
//import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

//import java.nio.file.Files;
//import java.nio.file.Path;
//import java.nio.file.Paths;
import org.apache.commons.io.*;

public class FileConverter {

    public static void ToFile(byte[] bytes, String pathName) throws IOException{
        FileOutputStream f = new FileOutputStream(pathName);
        f.write(bytes);
        f.close();
    }

    public static byte[] ToBytes(String pathName) throws IOException{
        //Path path = Paths.get(pathName);
        //byte[] bytes = Files.readAllBytes(path);

        //FileInputStream f = new FileInputStream(pathName);
        //byte[] bytes = IOUtils.toByteArray(f);

        File file = new File(pathName);
        byte[] bytes = FileUtils.readFileToByteArray(file);
        return bytes;
    }

}

my test class: 我的测试课:

import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;

public class FileConverterTest {

    @Test
    public void leesSchrijf() throws IOException{
        String test = "test";
        String pathName = "C://temp//testFile";
        byte[] bytes = test.getBytes();
        FileConverter.ToFile(bytes, pathName);
        Assert.assertEquals(bytes, FileConverter.ToBytes(pathName));
   } 
}

Whenever I execute the test class my results vary and the arrays never match. 每当我执行测试类时,我的结果都会变化,并且数组将永远不匹配。 eg java.lang.AssertionError: expected:<[B@a3a7a74> but was:<[B@53d5aeb> (first execution) 例如java.lang.AssertionError:预期:<[B @ a3a7a74>,但是:: <[B @ 53d5aeb>(首次执行)

java.lang.AssertionError: expected:<[B@29643eec> but was:<[B@745f0d2e> (next execution) java.lang.AssertionError:预期:<[B @ 29643eec>,但是是:<[B @ 745f0d2e>(下次执行)

I'm fairly new to test classes and I'm no java genius. 我是测试类的新手,而且我不是Java天才。 So I wondered whether I was doing something incorrectly. 所以我想知道我是否做错了什么。 If not, is there a way to preserve the byte array while writing it to a file? 如果不是,是否有一种在将字节数组写入文件时保留字节数组的方法?

In your Junit test you are comparing object references instead of object content. 在Junit测试中,您正在比较对象引用而不是对象内容。 What perhaps you wanted to achieve is to compare the content of byte arrays. 也许您想要实现的是比较字节数组的内容。 You can use Assert.assertArrayEquals by importing import org.junit.Assert; 您可以通过导入import org.junit.Assert;来使用Assert.assertArrayEquals import org.junit.Assert; Eg 例如

Assert.assertArrayEquals(bytes, FileConverter.ToBytes(pathName));

It seems that Assert.assertEquals is using euqlas method to compare objects, but arrays don't override its equals method, they inherit it from Object class and it is using == operator which compares references, not state of object. 看来Assert.assertEquals使用euqlas方法比较对象,但是数组没有覆盖其equals方法,它们从Object类继承它,并且它使用==运算符比较引用,而不是对象状态。

To compare content of arrays you need to iterate over them and compare their elements. 要比较数组的内容,您需要遍历它们并比较它们的元素。

You can also use Arrays.equals(arr1, arr2) helper method which will do it for you. 您也可以使用Arrays.equals(arr1, arr2)帮助程序方法来为您完成此工作。 There is also Arrays.deepEquals if you would want to compare multidimensional arrays. 如果要比较多维数组,还可以使用Arrays.deepEquals

You can use it like 你可以像这样使用它

Assert.assertTrue(Arrays.equals(arr1, arr2));

or in much cleaner way 或以更清洁的方式

Assert.assertArrayEquals(arr1, arr2);

还实现了Serializable接口,并在FileUtils类中生成一个私有的静态最终长serialVersionUID。

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

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