FileInputStream 类的read(byte [] b)方法从此文件输入流读取最大为b.length的字节数组。该方法将阻塞,直到有一些输入可用为止。
import java.io.File; import java.io.FileInputStream; public class Sample { public static void main(String args[]) throws Exception { int count =0; File file = new File("myData"); FileInputStream fis = new FileInputStream(file); byte[] bytesArray = new byte[(int)file.length()]; fis.read(bytesArray); String s = new String(bytesArray); System.out.println("给定文件的内容为:: " +new String(bytesArray)); } }
输出结果
给定文件的内容为:: Hi how are you welcome to nhooo