Here you can find the source of readFile(final File file)
Parameter | Description |
---|---|
file | File |
Parameter | Description |
---|---|
IOException | Signals that an I/O exception has occurred. |
public static byte[] readFile(final File file) throws IOException
//package com.java2s; /*//from w w w. jav a2 s.c o m * Copyright 2002-2016 Jalal Kiswani. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class Main { /** * Read file. * * @param file * File * @return byte[] * @throws IOException * Signals that an I/O exception has occurred. */ public static byte[] readFile(final File file) throws IOException { return readStream(new FileInputStream(file)); } /** * Read stream. * * @param inStream * InputStream * @return String * @throws IOException * Signals that an I/O exception has occurred. */ public static byte[] readStream(final InputStream inStream) throws IOException { DataInputStream in = null; try { in = new DataInputStream(inStream); final int size = in.available(); final byte arr[] = new byte[size]; in.readFully(arr); return arr; } finally { if (in != null) { in.close(); } } } }