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.

2
  • 1
    Please show us some code to help us understand Commented Jul 2, 2019 at 12:33
  • The short answer is that your code is not opening and reading the file. See the linked Q&A for ways to read a file into a byte array. Commented Jul 2, 2019 at 13:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.