Here you can find the source of saveFile(File file, String contents)
Parameter | Description |
---|---|
file | the given file target |
contents | the given contents |
Parameter | Description |
---|---|
IOException | if an IOException occurs while saving the file |
public static void saveFile(File file, String contents) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2007, 2012 IBM Corporation and others. * 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 * //from ww w. j a v a2s . co m * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class Main { /** * Save the given contents into the given file. The file parent folder must exist. * * @param file the given file target * @param contents the given contents * @throws IOException if an IOException occurs while saving the file */ public static void saveFile(File file, String contents) throws IOException { BufferedWriter writer = null; try { writer = new BufferedWriter(new FileWriter(file)); writer.write(contents); writer.flush(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { // ignore } } } } }