【问题标题】:write content in a byte array to a file and read the byte from file back to byte array将字节数组中的内容写入文件并将文件中的字节读回字节数组
【发布时间】:2015-04-08 15:40:05
【问题描述】:

如何在不改变之前写入的内容的情况下,将字节数组中的内容写入文件并将文件中的字节读回字节数组。在java中

【问题讨论】:

  • 只需以追加模式打开文件。使用这个构造函数FileWriter(String fileName, boolean append)
  • RTFM :: IO
  • @EddieB 如果您不知道答案,那么将某人指向错误的文档是非常无益的。
  • 使用FileOutputStream(File file, boolean append)
  • @Abhishek 没有。 File API 是古老的,应该避免使用。看我的回答。

标签: java bytearray


【解决方案1】:

有两种方法可以做到这一点,Files

final Path path myFile = Paths.get("path","to","file");
final byte[] toWrite = ...

Files.write(myFile, toWrite, StandardOpenOption.CREATE_NEW);

final byte[] read = Files.readAllBytes(myFile);

assert Arrays.equals(toWrite, read);

【讨论】:

    猜你喜欢
    • 2013-07-08
    • 2014-04-10
    • 1970-01-01
    • 2019-02-07
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多