Java Utililty Methods File Write nio

List of utility methods to do File Write nio

Description

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

Method

voidwriteFile(File file, String content)
write File
writeFile(new OutputStreamWriter(new FileOutputStream(file), StandardCharsets.UTF_8), content);
voidwriteFile(File file, String content)
write File
Files.write(file.toPath(), content.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.APPEND,
        StandardOpenOption.WRITE);
voidwriteFile(File file, String text)
write File
Files.write(text, file, Charset.defaultCharset());
voidwriteFile(FileObject fileObject, String content)
Create a file using the javax.annotation.processing.Filer , look the example bellow JavaFileObject fileObject = processingEnv.getFiler().createSourceFile(fileName, originatingElements);
OutputStreamWriter osw = null;
IOException exception = null;
try {
    OutputStream os = fileObject.openOutputStream();
    osw = new OutputStreamWriter(os, Charset.forName("UTF-8"));
    osw.write(content, 0, content.length());
} catch (IOException ex) {
    exception = ex;
...
voidwriteFile(List content, String targetFile)
Writes content to targetFile.
final PrintWriter writer = new PrintWriter(new FileWriter(new File(targetFile)));
try {
    for (String line : content) {
        writer.println(line);
} finally {
    writer.close();
voidwriteFile(OutputStream file, List data)
write File
BufferedWriter bufferedWriter = null;
try {
    bufferedWriter = new BufferedWriter(new OutputStreamWriter(file, Charset.defaultCharset()));
    for (String line : data) {
        bufferedWriter.write(line);
        bufferedWriter.write(S_NEWLINE);
} catch (Exception e) {
...
voidwriteFile(String filename, byte file[])
write File
java.nio.file.Path p = Paths.get(filename, new String[0]);
Files.write(p, file, new OpenOption[] { StandardOpenOption.CREATE });
voidwriteFile(String fileName, String content)
write File
try (OutputStream os = new FileOutputStream(fileName);
        PrintStream ps = new PrintStream(os, true, StandardCharsets.UTF_8.name())) {
    ps.print(content);
} catch (IOException e) {
    throw new IllegalStateException(e);
voidwriteFile(String fileName, String content)
write File
writeFile(fileName, new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8)));
voidwriteFile(String folder, String aFileName, String aItem)
write File
Path path = Paths.get(folder, aFileName);
try (BufferedWriter writer = Files.newBufferedWriter(path, ENCODING)) {
    writer.write(aItem);
    writer.newLine();