Java Write String to File writeFile(File f, String content, String encoding)

Here you can find the source of writeFile(File f, String content, String encoding)

Description

write File

License

Apache License

Declaration

public static void writeFile(File f, String content, String encoding) throws IOException 

Method Source Code

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

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStreamWriter;

import java.util.zip.GZIPOutputStream;

public class Main {
    private static final String DEFAULT_ENCODING = "UTF-8";

    public static void writeFile(File f, String content, String encoding) throws IOException {
        OutputStreamWriter out = null;
        try {//  www  .  j  av  a  2 s . c om
            if (f.getName().endsWith(".gz"))
                out = new OutputStreamWriter(new GZIPOutputStream(new FileOutputStream(f, false)), encoding);
            else
                out = new OutputStreamWriter(new FileOutputStream(f, false), encoding);
            out.append(content);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException err) {
                }
            }
        }
    }

    public static void writeFile(File f, String content) throws IOException {
        writeFile(f, content, DEFAULT_ENCODING);
    }
}

Related

  1. writeFile(File f, String body)
  2. writeFile(File f, String content)
  3. writeFile(File f, String content)
  4. writeFile(File f, String contents)
  5. writeFile(File f, String entry)
  6. writeFile(File f, String s)
  7. writeFile(File f, String str)