【发布时间】:2010-10-25 22:20:06
【问题描述】:
如何将java.io.File 转换为byte[]?
【问题讨论】:
-
我能想到的一个用途是从文件中读取序列化对象。
-
另一种是使用header查找文件类型。
-
试试这个 byte[] bytes = null; BufferedInputStream fileInputStream = null;尝试{文件文件 = 新文件(文件路径); fileInputStream = new BufferedInputStream(new FileInputStream(file)); //fileInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(this.filePath);字节 = 新字节[(int) file.length()]; fileInputStream.read(字节); } catch(FileNotFoundException ex){ 抛出 ex; }