Here you can find the source of saveFileFromString(File file, String encoding, String content)
Parameter | Description |
---|---|
file | java.io.File object to write string to |
encoding | file encoding. can be set to null |
content | file content as a String |
Parameter | Description |
---|---|
IOException | an exception |
public static void saveFileFromString(File file, String encoding, String content) throws IOException
//package com.java2s; /*/* w ww . j av a 2 s . c o m*/ _______________________ Apatar Open Source Data Integration Copyright (C) 2005-2007, Apatar, Inc. info@apatar.com 195 Meadow St., 2nd Floor Chicopee, MA 01013 ### This program is free software; you can redistribute it and/or modify ### it under the terms of the GNU General Public License as published by ### the Free Software Foundation; either version 2 of the License, or ### (at your option) any later version. ### This program is distributed in the hope that it will be useful, ### but WITHOUT ANY WARRANTY; without even the implied warranty of ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.# See the ### GNU General Public License for more details. ### You should have received a copy of the GNU General Public License along ### with this program; if not, write to the Free Software Foundation, Inc., ### 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ________________________ */ import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.OutputStreamWriter; public class Main { /** * writes string to a file * * @param file * java.io.File object to write string to * @param encoding * file encoding. can be set to null * @param content * file content as a String * @throws IOException */ public static void saveFileFromString(File file, String encoding, String content) throws IOException { if (content == null) { return; } char[] buf = new char[content.length()]; content.getChars(0, buf.length, buf, 0); OutputStreamWriter f = encoding == null ? new FileWriter(file) : new OutputStreamWriter(new FileOutputStream(file), encoding); try { f.write(buf); } finally { f.close(); } } }