-2

How can write content in a byte array to a file and read the byte from file back to byte array without changing the content written before. in java

6
  • Just open the file in append mode. use this constructor FileWriter(String fileName, boolean append) Commented Apr 8, 2015 at 7:20
  • RTFM :: IO Commented Apr 8, 2015 at 7:24
  • @EddieB if you don't know the answer then pointing someone to the wrong documentation is very unhelpful. Commented Apr 8, 2015 at 7:24
  • use FileOutputStream(File file, boolean append) Commented Apr 8, 2015 at 7:24
  • @Abhishek no. The File API is ancient and should be avoided. See my answer. Commented Apr 8, 2015 at 7:25

1 Answer 1

1

There are two methods for exactly this, in 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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.