Java Text File Write nio writeToFile(final File file, final String data)

Here you can find the source of writeToFile(final File file, final String data)

Description

Write a UTF-8 string to a file object.

License

Apache License

Declaration

public static void writeToFile(final File file, final String data) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    /**/*from   w  w w .  j av a 2 s  . c  om*/
     * Write a UTF-8 string to a file object.
     */
    public static void writeToFile(final File file, final String data) throws IOException {
        writeToFile(file.toPath(), data);
    }

    /**
     * Write a UTF-8 string to a path string.
     */
    public static void writeToFile(final String path, final String data) throws IOException {
        writeToFile(Paths.get(path), data);
    }

    /**
     * Write a UTF-8 string to a path object.
     */
    public static void writeToFile(final Path path, final String data) throws IOException {
        writeToFile(path, data.getBytes(StandardCharsets.UTF_8));
    }

    /**
     * Write bytes to a file object.
     */
    public static void writeToFile(final File file, final byte[] data) throws IOException {
        writeToFile(file.toPath(), data);
    }

    /**
     * Write bytes to a path string.
     */
    public static void writeToFile(final String path, final byte[] data) throws IOException {
        writeToFile(Paths.get(path), data);
    }

    /**
     * Write bytes to a path object.
     */
    public static void writeToFile(final Path path, final byte[] data) throws IOException {
        Files.write(path, data);
    }
}

Related

  1. writeTextFile(final File file, final String text)
  2. writeTextFile(String file, String articleContent)
  3. writeTo(File file, String content)
  4. writeToFile(File file, String content)
  5. writeToFile(File file, String str)
  6. writeToFile(String content, String fileName)
  7. writeToFile(String content, String fileName, boolean createFile)
  8. writeToFile(String filename, String fileContents)
  9. writeToFile(String filename, String... lines)