以字节流的形式读取文件

来源:互联网 发布:上海整合网络推广 编辑:程序博客网 时间:2024/06/06 12:20

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

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(一次性读取完毕)不适合作为缓冲区的初始长度,适合用于读取固定大小文件

萌新纯手打勿喷

阅读全文
1 0
原创粉丝点击