Java Write String to File writeFile(String fileName, String text, String characterEncoding)

Here you can find the source of writeFile(String fileName, String text, String characterEncoding)

Description

This will write a string to a file with the specified encoding

License

Open Source License

Declaration

public static void writeFile(String fileName, String text, String characterEncoding)
        throws UnsupportedEncodingException, FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2012 eBay Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from w  w  w. j  ava 2  s. co m
 *     eBay Inc. - initial API and implementation
 *******************************************************************************/

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

import java.io.UnsupportedEncodingException;

public class Main {
    /** This will write a string to a file with the specified encoding
     */
    public static void writeFile(String fileName, String text, String characterEncoding)
            throws UnsupportedEncodingException, FileNotFoundException, IOException {
        FileOutputStream fos;
        byte[] buf = text.getBytes(characterEncoding);
        int numWritten;
        fos = new FileOutputStream(fileName);
        numWritten = 0;
        fos.write(buf, numWritten, buf.length);
        fos.close();
    }
}

Related

  1. writeFile(String filename, String text)
  2. writeFile(String filename, String text)
  3. writeFile(String filename, String text)
  4. writeFile(String fileName, String text)
  5. writeFile(String filename, String text, boolean create)
  6. writeFile(String fileName, String textToSave)
  7. writeFile(String fileName, String[] contents)
  8. writeFile(String fileName, String[] lines)
  9. writeFile(String filename, StringBuilder sb)