Here you can find the source of saveFile(String path, String content)
Parameter | Description |
---|---|
path | Full path of the file ('c:/webapp/data.xml' or '/var/webapp/data.xml') |
content | Content to be saved in the file |
Parameter | Description |
---|
public static void saveFile(String path, String content) throws FileNotFoundException, IOException
//package com.java2s; /*//from w w w . ja v a 2 s.c om * Copyright 2007-2011 Nicolas Zozol * * 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. */ import java.io.*; public class Main { /** * @param path Full path of the file ('c:/webapp/data.xml' or '/var/webapp/data.xml') * @param content Content to be saved in the file * @throws java.io.FileNotFoundException * @throws java.io.IOException * @todo2 : It seems to save in only one line ! need to be tested */ public static void saveFile(String path, String content) throws FileNotFoundException, IOException { File f = new File(path); if (!f.exists()) { f.createNewFile(); } FileWriter fstream = null; BufferedWriter out = null; try { fstream = new FileWriter(path); out = new BufferedWriter(fstream); out.write(content); fstream.flush(); } finally { out.close(); fstream.close(); } } }