Here you can find the source of writeFile(File file, String data)
Parameter | Description |
---|---|
file | the file to write |
data | what to write to the file |
public static void writeFile(File file, String data)
//package com.java2s; /******************************************************************************* * Copyright (c) 2010, 2012 Institute for Dutch Lexicology * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.//from w w w. j av a 2s . c o m *******************************************************************************/ import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class Main { /** * The default encoding for opening files. */ private static String defaultEncoding = "utf-8"; /** * Write a String to a file. * @param file the file to write * @param data what to write to the file */ public static void writeFile(File file, String data) { PrintWriter out = openForWriting(file); try { out.print(data); } finally { out.close(); } } /** * Opens a file for writing in the default encoding. * * Wraps the Writer in a BufferedWriter and PrintWriter for efficient and convenient access. * * @param file * the file to open * @return write interface into the file */ public static PrintWriter openForWriting(File file) { return openForWriting(file, defaultEncoding); } /** * Opens a file for writing. * * Wraps the Writer in a BufferedWriter and PrintWriter for efficient and convenient access. * * @param file * the file to open * @param encoding * the encoding to use, e.g. "utf-8" * @return write interface into the file */ public static PrintWriter openForWriting(File file, String encoding) { try { return new PrintWriter( new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), encoding))); } catch (Exception e) { throw new RuntimeException(e); } } }