반응형

JAVA File to byte[]



private static byte[] loadFile(File file) throws IOException {

    InputStream is = new FileInputStream(file);

 

    long length = file.length();

    if (length > Integer.MAX_VALUE) {

        // File is too large

    }

    byte[] bytes = new byte[(int)length];

    

    int offset = 0;

    int numRead = 0;

    while (offset < bytes.length

           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {

        offset += numRead;

    }

 

    if (offset < bytes.length) {

        throw new IOException("Could not completely read file "+file.getName());

    }

 

    is.close();

    return bytes;

}



반응형

'java' 카테고리의 다른 글

JAVA 정규식 html javascript 삭제  (0) 2020.12.10
spring poi excel download  (0) 2020.10.22
[RCP] "org.eclipse.ui.ide.workbench" could not be found in the registry  (0) 2012.01.26
java File Delete  (0) 2011.03.03
java ReadLineEx  (0) 2010.07.26

+ Recent posts