0

I am trying read a text file and insert those content into database. My file.length() is 3540 But the byte array is full of zeros. As a result when I open the text file, it is empty.

File file = new File("/temp/abc.txt");
byte[] bytesArray = new byte[(int) file.length()]; 
databaseBean.setContentInByteArray(bytesArray);

Here the byteArray is full of zeroes.

1 Answers1

0
Path file = Paths.get("/temp/abc.txt");
byte[] bytesArray = Files.readAllBytes(path); 
databaseBean.setContentInByteArray(bytesArray);

A File is just a holder for a physical file on disk, a path, not its content.

new byte[42] will create a zeroed array of 42 bytes.

You would have to read those bytes. Path is a newer, more general class i.o. File and I used the Files class to read all those bytes.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138