与Java:

我有一个字节[],代表一个文件。

我如何写这个文件(即。C: \ myfile.pdf)

我知道它是用InputStream完成的,但我似乎无法解决它。


当前回答

使用Apache Commons IO

FileUtils.writeByteArrayToFile(new File("pathname"), myByteArray)

或者,如果你坚持为自己工作……

try (FileOutputStream fos = new FileOutputStream("pathname")) {
   fos.write(myByteArray);
   //fos.close(); There is no more need for this line since you had created the instance of "fos" inside the try. And this will automatically close the OutputStream
}

其他回答

你可以试试仙人掌:

new LengthOf(new TeeInput(array, new File("a.txt"))).value();

详情:http://www.yegor256.com/2017/06/22/object-oriented-input-output-in-cactoos.html

尝试OutputStream或者更具体地说FileOutputStream

我知道它是用InputStream完成的

实际上,你会写入一个文件输出…

////////////////////////// 1)文件字节 [] ///////////////////

Path path = Paths.get(p);
                    byte[] data = null;                         
                    try {
                        data = Files.readAllBytes(path);
                    } catch (IOException ex) {
                        Logger.getLogger(Agent1.class.getName()).log(Level.SEVERE, null, ex);
                    }

/////////////////////// 2] Byte[]文件 ///////////////////////////

 File f = new File(fileName);
 byte[] fileContent = msg.getByteSequenceContent();
Path path = Paths.get(f.getAbsolutePath());
                            try {
                                Files.write(path, fileContent);
                            } catch (IOException ex) {
                                Logger.getLogger(Agent2.class.getName()).log(Level.SEVERE, null, ex);
                            }

基本的例子:

String fileName = "file.test";

BufferedOutputStream bs = null;

try {

    FileOutputStream fs = new FileOutputStream(new File(fileName));
    bs = new BufferedOutputStream(fs);
    bs.write(byte_array);
    bs.close();
    bs = null;

} catch (Exception e) {
    e.printStackTrace()
}

if (bs != null) try { bs.close(); } catch (Exception e) {}