Here you can find the source of writeStringToFile(File file, String contents)
Parameter | Description |
---|---|
IOException | an exception |
public static void writeStringToFile(File file, String contents) throws IOException
//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) { } } } }