Here you can find the source of writeFile(File file, String data)
Parameter | Description |
---|---|
file | File path to save the data. |
data | String data to save. |
Parameter | Description |
---|
public static void writeFile(File file, String data) throws FileNotFoundException, IOException
//package com.java2s; /*/*w w w . j a v a2 s.c o m*/ * Project: Buddata ebXML RegRep * Class: FileUtil.java * Copyright (C) 2008 Yaman Ustuntas * * 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 3 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, see <http://www.gnu.org/licenses/>. */ import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; public class Main { /** * Writes String data to the given path (UTF-8). * * @param file File path to save the data. * @param data String data to save. * @throws java.io.FileNotFoundException * @throws java.io.IOException */ public static void writeFile(File file, String data) throws FileNotFoundException, IOException { writeFile(file, data.getBytes("UTF-8")); } /** * Writes data byte array to the given path. * * @param file File path to save the data. * @param data Data to write in the file. * @throws java.io.FileNotFoundException * @throws java.io.IOException */ public static void writeFile(File file, byte[] data) throws FileNotFoundException, IOException { writeFile(file, new ByteArrayInputStream(data)); } /** * Write data from an <code>InputStream</code> to file. * * @param file File to write to. * @param is Stream to write. * @throws java.io.IOException */ public static void writeFile(File file, InputStream is) throws IOException { FileOutputStream fos = new FileOutputStream(file); byte[] buf = new byte[2048]; int count = 0; while ((count = is.read(buf)) > 0) { fos.write(buf, 0, count); } fos.flush(); fos.close(); is.close(); } }