2

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). 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. e.g. java.lang.AssertionError: expected:<[B@a3a7a74> but was:<[B@53d5aeb> (first execution)

java.lang.AssertionError: expected:<[B@29643eec> but was:<[B@745f0d2e> (next execution)

I'm fairly new to test classes and I'm no java genius. 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?

Community
  • 1
  • 1
sdp2718
  • 23
  • 4
  • looking at your error message, i am starting to wonder if there is a problem with the conversion. In fact bytes.length=test.length(). Try this.A byte is a Character. –  May 03 '15 at 12:14
  • You're not comparing the contents of the byte arrays. – user207421 May 03 '15 at 19:06

3 Answers3

4

In your Junit test you are comparing object references instead of object content. 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; E.g.

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

sol4me
  • 15,233
  • 5
  • 34
  • 34
1

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.

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. There is also Arrays.deepEquals if you would want to compare multidimensional arrays.

You can use it like

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

or in much cleaner way

Assert.assertArrayEquals(arr1, arr2);
Pshemo
  • 122,468
  • 25
  • 185
  • 269
0

also implements Serializable interface and generate a private static final long serialVersionUID in your FileUtils class.

kavi temre
  • 1,321
  • 2
  • 14
  • 21