Java Utililty Methods Path File Write nio

List of utility methods to do Path File Write nio

Description

The list of methods to do Path File Write nio are organized into topic(s).

Method

voidwriteFully(Path filePath, byte[] bytes)
write Fully
writeFully(new FileOutputStream(filePath.toFile()), bytes);
StringwriteJaasConfig(File krbDir, String princ, String keytabPath)
write Jaas Config
File file = File.createTempFile("jaas_", ".conf", krbDir);
file.deleteOnExit();
String jaasData;
jaasData = "Client {\n";
jaasData += "com.sun.security.auth.module.Krb5LoginModule required\n";
jaasData += "principal=\"" + princ + "\"\n";
jaasData += "useKeyTab=true\n";
jaasData += "keyTab=\"" + keytabPath + "\"\n";
...
voidwriteJson(Object data, Path path)
Writes the data as a JSON file at the given path
if (data == null || path == null) {
    return;
ObjectMapper jsonMapper = new ObjectMapper();
jsonMapper.writeValue(path.toFile(), data);
voidwriteLastReadDate(String sinceDate, Path sinceFile)
Simple helper to write the given String into a file
ArrayList<String> lines = new ArrayList<String>();
lines.add(sinceDate);
Files.write(sinceFile, lines, Charset.forName("UTF-8"));
voidwriteLines(Path path, T... a)
write Lines
Files.write(path, Arrays.asList(a), StandardCharsets.UTF_8);
voidwriteLineS(String filePath, String msg, int line)
Write data to a specific line of a text file.
File f = new File(filePath);
List<String> lines = Files.readAllLines(f.toPath());
lines.set(line - 1, msg);
Files.write(f.toPath(), lines);
voidwriteOrAppendLinesToFile(boolean append, Path filePath, String... lines)
write Or Append Lines To File
File file = filePath.toFile();
if (!file.exists() && !file.createNewFile()) {
    return;
PrintWriter printWriter = new PrintWriter(Files.newBufferedWriter(file.toPath(), UTF_8,
        append ? new StandardOpenOption[] { CREATE, APPEND } : new StandardOpenOption[] { CREATE }));
for (String line : lines) {
    printWriter.println(line);
...
voidwriteOutputToFile(Path path)
write Output To File
BufferedWriter bw = Files.newBufferedWriter(path, Charset.forName("UTF-8"),
        new OpenOption[] { StandardOpenOption.CREATE });
bw.append(consoleOutputCache.toString());
bw.close();
voidwriteString(Path path, String str)
write String
byte[] bytes = str.getBytes("utf-8");
Files.write(path, bytes, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
voidwriteString(String path, String data)
write String
try (BufferedWriter writer = new BufferedWriter(
        new OutputStreamWriter(new FileOutputStream(path), StandardCharsets.UTF_8))) {
    writer.write(data);