Here you can find the source of readFile(String path)
Parameter | Description |
---|---|
path | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static byte[] readFile(String path) throws IOException
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; public class Main { /**/*ww w. j a v a 2s .c om*/ * read file to byte array * * @param path * @return * @throws IOException */ public static byte[] readFile(String path) throws IOException { final File file = new File(path); if (!file.exists()) throw new IOException("the path" + path + " is not exists"); final FileInputStream templateImageData = new FileInputStream(file); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] cache = new byte[255]; int len = 0; while (-1 != (len = templateImageData.read(cache))) { outputStream.write(cache, 0, len); } outputStream.flush(); outputStream.close(); templateImageData.close(); return outputStream.toByteArray(); } }