Java中将文件转换成byte[]

919

如何将 java.io.File 转换为 byte[]


我能想到的一个用途是从文件中读取序列化对象。 - Mahm00d
2
另一种方法是使用文件头查找文件类型。 - James P.
尝试以下代码:byte[] bytes = null; BufferedInputStream fileInputStream = null; try { File file = new File(filePath); fileInputStream = new BufferedInputStream(new FileInputStream(file)); //fileInputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(this.filePath); bytes = new byte[(int) file.length()]; fileInputStream.read(bytes); } catch (FileNotFoundException ex) { throw ex; } - Rohit Chaurasiya
27个回答

1494

JDK 7开始,您可以使用Files.readAllBytes(Path)方法。

示例:

import java.io.File;
import java.nio.file.Files;

File file;
// ...(file is initialised)...
byte[] fileContent = Files.readAllBytes(file.toPath());

85
JDK7引入了File.toPath()方法,可以返回一个Path对象。 - KevinL
10
您可以从文件获取路径。尝试使用以下代码:File file = new File("/path"); Path path = Paths.get(file.getAbsolutePath()); byte[] data = Files.readAllBytes(path); - gfelisberto
4
Java.nio中如何处理文件关闭 - 换句话说,上面的代码是否需要关闭某些内容? - akauppi
6
请参见答案中的链接:“该方法确保文件已关闭...” - Bernhard Barker
4
如果目标SDK版本为26或更高,则此功能现在在Android上可用。 - James Warner
显示剩余3条评论

580

36
非常正确!我宁愿多些代码行数,也不想再增加依赖。依赖有隐藏成本。你需要与库保持更新,将其包含在构建脚本中,向使用你的代码的人进行沟通等等。如果你已经使用了一个有相关代码的库,那就用它吧,否则我会建议你自己编写。 - Stijn de Witt
12
这回答了如何读取文件的问题,但并不回答如何将类型为 java.IO.File 的对象转换为 byte[]。 - Ingo
6
怎么将一个 File 读取成 byte[] 数组?由于使用的是 Java6,因此无法使用 NIO 方法 :( - P A S T R Y
4
@ymajoros,您能否分享任何“标准的三行解决方案”给我们,这样我们就不必依赖反复发明轮子的依赖项了? - matteo
3
@matteo:有没有其它的方法?例如,Files.readAllBytes()。这种方法简单,无需依赖。 - ymajoros
显示剩余5条评论

320

自 JDK 7 开始 - 一行代码:

byte[] array = Files.readAllBytes(Paths.get("/path/to/file"));

无需外部依赖。


22
现在这是一个比被接受答案更好的选择,因为它不需要使用Apache Commons。 - james.garriss
1
我也需要这个:String text = new String(Files.readAllBytes(new File("/path/to/file").toPath()));,它最初来自于https://dev59.com/G3A75IYBdhLWcg3wPmd8#26888713。 - cgl
9
在安卓中,它需要最低API级别为26。 - Ashutosh Chamoli
3
如果你还没有添加,你需要加入 import java.nio.file.Files;import java.nio.file.Paths; - Sam
这对于Java8也很有用。 - Mugeesh Husain

171

2
你必须检查f.read()的返回值。有时候会出现这样的情况,你可能没有读取整个文件。 - bugs_
8
只有在你读文件时文件正在被修改,才会出现这种情况。在所有其他情况下,都会抛出IOException异常。为了解决这个问题,建议以读写模式打开文件:RandomAccessFile(fileName, "rw")。 - Dmitry Mitskevich
5
我可以想象其他读取文件部分内容的途径(该文件位于网络共享中...),readFully()符合你正在寻找的合同要求。 - DThought
3
请记住,RandomAccessFile不是线程安全的。因此,在某些情况下可能需要同步。 - bancer
@DmitryMitskevich 还有其他情况,可能是非标准的文件系统。例如,在Linux上读取/proc/中的“文件”可能会导致短读取(即,您需要循环读取所有内容)。 - nos
这个答案似乎对于大约5m大小的文件比nio或者streams更快。 - Chaffers

85

基本上你需要将它读入内存。打开文件,分配数组,并将文件内容读入数组。

最简单的方法类似于以下内容:

public byte[] read(File file) throws IOException, FileTooBigException {
    if (file.length() > MAX_FILE_SIZE) {
        throw new FileTooBigException(file);
    }
    ByteArrayOutputStream ous = null;
    InputStream ios = null;
    try {
        byte[] buffer = new byte[4096];
        ous = new ByteArrayOutputStream();
        ios = new FileInputStream(file);
        int read = 0;
        while ((read = ios.read(buffer)) != -1) {
            ous.write(buffer, 0, read);
        }
    }finally {
        try {
            if (ous != null)
                ous.close();
        } catch (IOException e) {
        }

        try {
            if (ios != null)
                ios.close();
        } catch (IOException e) {
        }
    }
    return ous.toByteArray();
}

这段代码存在一些不必要的文件内容复制(实际上数据被复制了三次:从文件到buffer,从bufferByteArrayOutputStream,从ByteArrayOutputStream到实际的结果数组)。

你还需要确保只在内存中读取特定大小的文件(通常这取决于应用程序):-)。

你还需要在函数外部处理IOException异常。

另一种方法是:

public byte[] read(File file) throws IOException, FileTooBigException {
    if (file.length() > MAX_FILE_SIZE) {
        throw new FileTooBigException(file);
    }

    byte[] buffer = new byte[(int) file.length()];
    InputStream ios = null;
    try {
        ios = new FileInputStream(file);
        if (ios.read(buffer) == -1) {
            throw new IOException(
                    "EOF reached while trying to read the whole file");
        }
    } finally {
        try {
            if (ios != null)
                ios.close();
        } catch (IOException e) {
        }
    }
    return buffer;
}

这里没有不必要的复制。

FileTooBigException 是一个自定义的应用程序异常。 MAX_FILE_SIZE 常量是应用程序参数。

对于大文件,您可能应该考虑使用流处理算法或使用内存映射(请参见 java.nio)。


iOS需要在try之外声明。 - Daryl Spitzer
第二个例子中的语句“ios.read(buffer)”只会读取文件的前4096个字节(假设与第一个例子中使用的4k缓冲区相同)。为了使第二个例子正常工作,我认为读取操作必须在while循环内进行,并检查结果是否为-1(已到达文件结尾)。 - Stijn de Witt
抱歉,撤回我之前的评论,错过了将缓冲区设置为文件长度的声明。不过,我仍然更喜欢第一个示例的方式。一次性将整个文件读入缓冲区是不可扩展的。当文件很大时,您将面临内存耗尽的风险。 - Stijn de Witt
“最简单”的方法是使用try-with-resources。 - Sina Madani
很酷,但有点啰嗦。 - Sapphire_Brick

79

正如有人所说,Apache Commons File Utils可能具有您正在寻找的功能

public static byte[] readFileToByteArray(File file) throws IOException

示例用法 (Program.java):

import org.apache.commons.io.FileUtils;
public class Program {
    public static void main(String[] args) throws IOException {
        File file = new File(args[0]);  // assume args[0] is the path to file
        byte[] data = FileUtils.readFileToByteArray(file);
        ...
    }
}

在Maven仓库中(https://mvnrepository.com/artifact/commons-io/commons-io) - Nick Dong

28
如果您没有Java 8,并且同意我认为为了避免编写几行代码而包含一个大型库是一个不好的想法:
public static byte[] readBytes(InputStream inputStream) throws IOException {
    byte[] b = new byte[1024];
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    int c;
    while ((c = inputStream.read(b)) != -1) {
        os.write(b, 0, c);
    }
    return os.toByteArray();
}

调用方负责关闭流。


谢谢。这就是我需要的。 - Nguyen Minh Hien

26
// Returns the contents of the file in a byte array.
    public static byte[] getBytesFromFile(File file) throws IOException {        
        // Get the size of the file
        long length = file.length();

        // You cannot create an array using a long type.
        // It needs to be an int type.
        // Before converting to an int type, check
        // to ensure that file is not larger than Integer.MAX_VALUE.
        if (length > Integer.MAX_VALUE) {
            // File is too large
            throw new IOException("File is too large!");
        }

        // Create the byte array to hold the data
        byte[] bytes = new byte[(int)length];

        // Read in the bytes
        int offset = 0;
        int numRead = 0;

        InputStream is = new FileInputStream(file);
        try {
            while (offset < bytes.length
                   && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
                offset += numRead;
            }
        } finally {
            is.close();
        }

        // Ensure all the bytes have been read in
        if (offset < bytes.length) {
            throw new IOException("Could not completely read file "+file.getName());
        }
        return bytes;
    }

此外,将numRead放在循环内部。在最小有效范围内声明变量。将其放在while循环之外仅是为了启用复杂的“while”测试;最好在循环内部进行EOF测试(如果发生EOFException,则抛出异常)。 - erickson
抛出新的IOException("文件太大了!")异常!当文件太大时,我们应该怎么办?还有关于此的任何示例吗? - Fer

25

您也可以使用NIO API来实现。只要文件总大小(以字节为单位)适合int,我就可以使用以下代码完成此操作。

File f = new File("c:\\wscp.script");
FileInputStream fin = null;
FileChannel ch = null;
try {
    fin = new FileInputStream(f);
    ch = fin.getChannel();
    int size = (int) ch.size();
    MappedByteBuffer buf = ch.map(MapMode.READ_ONLY, 0, size);
    byte[] bytes = new byte[size];
    buf.get(bytes);

} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} finally {
    try {
        if (fin != null) {
            fin.close();
        }
        if (ch != null) {
            ch.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我认为它非常快,因为它使用了MappedByteBuffer。


2
如果您只打算读取文件一次,那么绝对没有必要使用内存映射,因为这将使用比使用普通的FileInputStream多两倍的内存。 - james
1
不幸的是,MappedByteBuffer没有自动释放。 - Tom Hawtin - tackline
2
太棒了,新的示例包括printStackTrace,经典的异常处理已经失效。 - james
我同意。这是Eclipse自带的默认内容。我觉得我应该重新抛出异常! - Amit
我一直在对nio进行基准测试,以便从文件创建byte[]。除了使用直接缓冲区外,它确实需要两倍的内存。虽然对于非常大的文件(大约比缓冲IO快两倍的200M),它更快,但对于大约5M的文件,它似乎输了5倍。 - Chaffers

24

简单的方法:

File fff = new File("/path/to/file");
FileInputStream fileInputStream = new FileInputStream(fff);

// int byteLength = fff.length(); 

// In android the result of file.length() is long
long byteLength = fff.length(); // byte count of the file-content

byte[] filecontent = new byte[(int) byteLength];
fileInputStream.read(filecontent, 0, (int) byteLength);

有更简单的方法,比如已经提到的一行代码。 - Sapphire_Brick
1
@Sapphire_Brick 有更简单的方法,但是一行代码并不适用于所有情况,比如安卓系统。 - Behr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接