Here you can find the source of fileToByteArray(String path)
Parameter | Description |
---|---|
path | a parameter |
public static byte[] fileToByteArray(String path)
//package com.java2s; //License from project: Open Source License import java.io.*; public class Main { /**/*from ww w . ja v a2 s . com*/ * Returns the file at the specified path as a byte array. * @param path * @return */ public static byte[] fileToByteArray(String path) { File file = new File(path); if (!file.exists() || file.isDirectory()) { return null; } FileInputStream in = null; try { byte[] array = new byte[(int) file.length()]; in = new FileInputStream(file); in.read(array); return array; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } /** * Returns whether or not the specified path is a directory. * @param path the file path * @return <i>true</i> if path is directory */ public static boolean isDirectory(String path) { return new File(path).isDirectory(); } }