Java FileWriter Write saveFile(String path, String content)

Here you can find the source of saveFile(String path, String content)

Description

save File

License

Apache License

Parameter

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

Exception

Parameter Description

Declaration

public static void saveFile(String path, String content) throws FileNotFoundException, IOException 

Method Source Code


//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();
        }
    }
}

Related

  1. saveFile(File file, String text, boolean append)
  2. saveFile(List contents, String fileName)
  3. saveFile(String filePath, String content)
  4. saveFile(String filePath, String data)
  5. saveFile(String path, Map> setup)
  6. SaveFile(String path, String data, boolean deleteOnExit)
  7. saveFile(String path, String sb)
  8. saveFile(String[] lines, String savedName, String extension)
  9. saveFileContents(File file, String contents)