Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从/向文件读取和写入字节_Java_Arrays_File_Byte - Fatal编程技术网

Java 从/向文件读取和写入字节

Java 从/向文件读取和写入字节,java,arrays,file,byte,Java,Arrays,File,Byte,我试图将字节数组写入文件,然后读取它们。重要的是,我写入的字节数组与我读取的字节数组相同。我尝试了这里建议的一些方法()。然而,当我应用它们时,我最终读取了我最初编写的另一个数组 以下是我尝试过的: import java.io.File; //import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; //import java.nio.file.Files; //i

我试图将字节数组写入文件,然后读取它们。重要的是,我写入的字节数组与我读取的字节数组相同。我尝试了这里建议的一些方法()。然而,当我应用它们时,我最终读取了我最初编写的另一个数组

以下是我尝试过的:

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

}
我的测试班:

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));
   } 
}
每当我执行测试类时,我的结果都会发生变化,数组永远不会匹配。 e、 g.java.lang.AssertionError:应为:但为:(第一次执行)

java.lang.AssertionError:应为:但为:(下一次执行)


我是测试类的新手,我不是java天才。所以我想知道我是否做错了什么。如果没有,在将字节数组写入文件时是否有办法保留它?

在Junit测试中,您比较的是对象引用而不是对象内容。您可能想要实现的是比较字节数组的内容。您可以通过导入
import org.junit.Assert来使用
Assert.assertarayequals
例如


Assert.assertArrayEquals(字节,FileConverter.ToBytes(路径名))

似乎
Assert.assertEquals
正在使用
euqlas
方法来比较对象,但数组不重写其
equals
方法,它们从
Object
类继承它,并使用
=
操作符来比较引用,而不是对象的状态

要比较数组的内容,您需要遍历它们并比较它们的元素

您还可以使用
Arrays.equals(arr1,arr2)
helper方法,该方法将为您执行此操作。如果要比较多维数组,还有
数组。deepEquals

你可以像这样使用它

Assert.assertTrue(Arrays.equals(arr1, arr2));
或者以更干净的方式

Assert.assertArrayEquals(arr1, arr2);

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

查看错误消息,我开始怀疑转换是否有问题。实际上,bytes.length=test.length()。试试这个。字节是一个字符。你没有比较字节数组的内容。