Java Text File Write nio writeContent(File file, String content)

Here you can find the source of writeContent(File file, String content)

Description

Writes the string content to the file.

License

Apache License

Parameter

Parameter Description
file a parameter
content a parameter

Declaration

public static void writeContent(File file, String content) 

Method Source Code

//package com.java2s;
/*//ww  w . ja  v  a  2  s .  c o  m
 * Copyright 2011 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.BufferedWriter;
import java.io.File;

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

import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

public class Main {
    /**
     * Writes the string content to the file.
     *
     * @param file
     * @param content
     */
    public static void writeContent(File file, String content) {
        OutputStreamWriter os = null;
        try {
            os = new OutputStreamWriter(new FileOutputStream(file),
                    Charset.forName("UTF-8"));
            BufferedWriter writer = new BufferedWriter(os);
            writer.append(content);
            writer.flush();
        } catch (Throwable t) {
            System.err.println("Failed to write content of "
                    + file.getAbsolutePath());
            t.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException ioe) {
                    System.err.println("Failed to close file "
                            + file.getAbsolutePath());
                    ioe.printStackTrace();
                }
            }
        }
    }
}

Related

  1. writeAllLines(File file, List list)
  2. writeAllText(File file, String contents)
  3. writeArray2File(File file, Object[] objects, String code)
  4. writeAscii(CharSequence ascii, OutputStream os)
  5. writeContent(File file, String content)
  6. writeData(String address, BigInteger toWrite)
  7. writeDataFromList(String address, ArrayList toWrite)
  8. writeFileContent(File file, String content)
  9. writeFileContents(File file, String content)