Create new file and append byte array to it with java.nio.file.Files in Java
Description
The following code shows how to create new file and append byte array to it with java.nio.file.Files.
Example
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
/*from w ww . j a va2 s . c o m*/
public class Main {
public static void main(String[] args) throws IOException {
Path path = Paths.get("/users.txt");
byte[] contents = Files.readAllBytes(path);
Path newPath = Paths.get("/newUsers.txt");
byte[] newContents = "aaa".getBytes();
Files.write(newPath, contents, StandardOpenOption.CREATE);
Files.write(newPath, newContents, StandardOpenOption.APPEND);
}
}