Java Text File Write nio write(File file, String data, boolean append)

Here you can find the source of write(File file, String data, boolean append)

Description

write

License

BSD License

Declaration

public static void write(File file, String data, boolean append) throws IOException 

Method Source Code

//package com.java2s;
/* Copyright ? 2015 Gerald Rosenberg.
 * Use of this source code is governed by a BSD-style
 * license that can be found in the License.md file.
 *//*from   w w  w.  j  ava  2 s .  c o m*/

import java.io.File;
import java.io.IOException;

import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;

import java.nio.file.StandardOpenOption;

import java.util.HashSet;

import java.util.Set;

public class Main {
    public static void write(File file, String data, boolean append) throws IOException {
        Set<OpenOption> options = new HashSet<OpenOption>();
        options.add(StandardOpenOption.CREATE);
        options.add(StandardOpenOption.WRITE);
        if (append) {
            options.add(StandardOpenOption.APPEND);
        } else {
            options.add(StandardOpenOption.TRUNCATE_EXISTING);
        }
        write(file, data, options.toArray(new OpenOption[options.size()]));
    }

    private static void write(File file, String data, OpenOption... options) throws IOException {
        Path path = file.toPath();
        Files.write(path, data.getBytes(), options);
    }
}

Related

  1. write(File file, List lines, StandardOpenOption... options)
  2. write(File file, String content)
  3. write(final Map dataBase, final File currentFile)
  4. write(final Object o, final String filename)
  5. write(final OutputStream outputStream, final String string)
  6. write(final StringBuffer data, final Writer output)