以字节流的形式读取文件

根据路径来读取文件(字节流)

public static byte[] readFile(String path){
    File file = new File(path);
    FileInputStream input = null;
    try{
        input = new FileInputStream(file);
        byte[] buf =new byte[input.available()];
        input.read(buf);
        }catcha(Exception e){
            e.printstackTrack();
        }finally{
            if(input != null){
            try {
                input.close();
                }catch(IOException e){
                e.printStackTrace();
        }
        }
    }
    return null;
}

这样就可以直接调用类名,把文件变成字节流了,例如获取这个文件的字节流

String a = readFile("c:/a.word");
System.out.println(a);

注意:.available(一次性读取完毕)不适合作为缓冲区的初始长度,适合用于读取固定大小文件

萌新纯手打勿喷

你可能感兴趣的:(JAVA基础)