Java Text File Write writeStringToFile(File file, String contents)

Here you can find the source of writeStringToFile(File file, String contents)

Description

Stores a string into an ordinary workspace file in UTF8 format.

License

Open Source License

Exception

Parameter Description
IOException an exception

Declaration

public static void writeStringToFile(File file, String contents) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005, 2009 BEA Systems, Inc.
 * 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:/*www  .  j a va 2s .c o  m*/
 *    tyeung@bea.com - initial API and implementation
 *******************************************************************************/

import java.io.BufferedOutputStream;

import java.io.File;

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

import java.io.OutputStream;

public class Main {
    /**
     * Stores a string into an ordinary workspace file in UTF8 format.
     * The file will be created if it does not already exist.
     * @throws IOException
     */
    public static void writeStringToFile(File file, String contents) throws IOException {
        byte[] data = contents.getBytes("UTF8"); //$NON-NLS-1$
        OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
        try {
            for (byte b : data) {
                out.write(b);
            }
        } finally {
            try {
                out.close();
            } catch (IOException ioe) {
            }
        }
    }
}

Related

  1. writeStringToFile(File f, String s)
  2. writeStringToFile(File f, String s)
  3. writeStringToFile(File file, String content)
  4. writeStringToFile(File file, String content)
  5. writeStringToFile(File file, String content)
  6. writeStringToFile(File file, String data, String encoding)
  7. writeStringToFile(File file, String data, String encoding)
  8. writeStringToFile(File file, String data, String encoding)
  9. writeStringToFile(File file, String data, String encoding)