简体   繁体   English

Java-使用java7将文件转换为Byte数组,反之亦然

[英]Java- Conversion of a file into a Byte array and vice-versa using java7

Please explain me by giving an example of the following syntax: 请通过以下语法示例向我解释:

static byte[] readAllBytes(Path path)
static Path write(Path path, byte[] bytes, OpenOption... options)

I have a rough idea about path,bytes but OpenOption! 我对路径,字节数有一个粗略的了解,但对OpenOption却很了解! So an example will be best. 因此,最好的例子是。 The code is related to java 7. 该代码与Java 7有关。

There is such an example in the Files class documentation . Files类文档中有这样的示例。

 Path path = ...
 byte[] bytes = ...
 Files.write(path, bytes, StandardOpenOption.APPEND);

Code example: 代码示例:

Path path = Paths.get(pathToFile);
byte[] bArray = new byte[0];
try {
    bArray = Files.readAllBytes(path);
} catch (IOException e) {
    e.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM