Convert byteArray from fileInputStream

Viewed 92

I wanna read huge file(more then GB) and convert that as byte array in order to store the file dB.

The problem i am facing is when i convert file to byte array or reading the file i am getting Java heap memory run-time exception

I want to know what is the best way to read file and convert to byte array without being using more memory

I have googled i found that IOUtils provide better performance but i tried that it dint help me. I using java 8

private void readFile() throws IOException {

        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("C:\test.mp4")
            byte[] bytes = IOUtils.toByteArray(fileInputStream);
            Base64 codec = new Base64();
            byte[] decoded = codec.encode(bytes);

                                            fileInputStream.close();
}

Can you please help to convert that file to byte array with best approach?

1 Answers

When you are saying that you want to write to db, avoid reading into an array in memory. Straight update to db using setCharacterStream.

Connection conn = //... initialize connection
PreparedStmt stmt = null;
try {
    stmt = conn.prepareStatement(this.insertQuerySql);
    stmt.setLong(1, varLong); 
    stmt.setString(2, varString);
    stmt.setString(3, varString2);
    File file = new File(filePathToVeryLargeFileInGBs);
    FileReader fileReader = new FileReader(file);
    pstmt.setCharacterStream(4, fileReader, Files.size(Paths.get(filePathToVeryLargeFileInGBs);
    pstmt.executeUpdate();
} catch(Exception e) {
    System.err.println(e);
    e.printStackTrace();
} finally {
    if (stmt != null ) { 
        stmt.close();
    }   
    if (conn != null ) { 
        try {
            conn.close();
        }   
        catch(Exception e2) {
            e2.printStackTrace();
        }   
    }   
}

If you must read it into a byte[] Then the better option is to use:

byte[] bArr = ByteBuffer.wrap(FileUtils.readFileToByteArray(new File(filePathToVeryLargeFileInGBs)));
Related