Java Path File Write nio writeOrAppendLinesToFile(boolean append, Path filePath, String... lines)

Here you can find the source of writeOrAppendLinesToFile(boolean append, Path filePath, String... lines)

Description

write Or Append Lines To File

License

Open Source License

Declaration

private static void writeOrAppendLinesToFile(boolean append, Path filePath, String... lines)
            throws IOException 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class Main {
    private static void writeOrAppendLinesToFile(boolean append, Path filePath, String... lines)
            throws IOException {
        File file = filePath.toFile();
        if (!file.exists() && !file.createNewFile()) {
            return;
        }//  w  ww . j  ava2s . co  m

        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);
        }
        printWriter.close();
    }
}

Related

  1. writeJaasConfig(File krbDir, String princ, String keytabPath)
  2. writeJson(Object data, Path path)
  3. writeLastReadDate(String sinceDate, Path sinceFile)
  4. writeLines(Path path, T... a)
  5. writeLineS(String filePath, String msg, int line)
  6. writeOutputToFile(Path path)
  7. writeString(Path path, String str)
  8. writeString(String path, String data)
  9. writeStringAndNewLineToBufferedWriter(String line, BufferedWriter writer, Path path)