Java UTF File Write getUTF8FileWriter(File f)

Here you can find the source of getUTF8FileWriter(File f)

Description

get UTF File Writer

License

Open Source License

Declaration

public static Writer getUTF8FileWriter(File f) throws FileNotFoundException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.*;

import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
    /**//from w  ww  .j  a  v a  2  s  .com
     * current working directory. Can be changed if needed
     */
    private static String cwd = ".";

    public static Writer getUTF8FileWriter(File f) throws FileNotFoundException {
        return getUTF8FileWriter(f.getPath());
    }

    public static Writer getUTF8FileWriter(String filename) throws FileNotFoundException {
        return new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(getFile(filename)), StandardCharsets.UTF_8));
    }

    public static Path getPath(String path) {
        Path p = Paths.get(path);
        if (p.isAbsolute())
            return p;
        else
            // making this absolute protects against the CWD being added twice
            return Paths.get(cwd, path).toAbsolutePath();
    }

    public static File getFile(String path) {
        return getPath(path).toFile();
    }
}

Related

  1. asPrintWriterUTF8(OutputStream out)
  2. getBufferedUTF8Writer(File file)
  3. getUTF8FileAppendWriter(String filename)
  4. getUTF8Writer(OutputStream os)
  5. getUTF8Writer(String file)
  6. utf8Writer(File file)
  7. utf8Writer(final File f)