Java Write String to File writeFile(String fileName, String output)

Here you can find the source of writeFile(String fileName, String output)

Description

Write a String to a file

License

Apache License

Parameter

Parameter Description
fileName a parameter
output a parameter

Exception

Parameter Description

Declaration

public static void writeFile(String fileName, String output) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
/*/*from  www. j  av a  2s .c o m*/
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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 {
    /**
    * Write a String to a file
    * @param fileName
    * @param output
    * @throws java.io.IOException
    */
    public static void writeFile(String fileName, String output) throws FileNotFoundException, IOException {
        File file = openFile(fileName);
        if (file == null) {
            throw new FileNotFoundException(fileName);
        }

        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF8"));
        try {
            out.write(output);
            out.flush();
        } finally {
            out.close();
        }
    }

    /**
     * Return an instance of File...
     *
     * @param path
     * @return File
     */
    public static File openFile(String path) {
        try {
            return new File(path);
        } catch (Error e) // J#.NET throws an error if the file is not found...
        {
            return null;
        }
    }

    public static File openFile(String path, boolean mkdir) {
        File f = new File(path).getAbsoluteFile();

        if (mkdir) {
            new File(f.getParent()).mkdirs();
        }

        return f;
    }

    /**
     * Return an instance of File...
     *
     * @param parentPath
     * @param fileName
     * @return File
     */
    public static File openFile(File parentPath, String fileName) {
        try {
            return new File(parentPath, fileName);
        } catch (Error e) // J#.NET throws an error if the file is not found...
        {
            return null;
        }
    }
}

Related

  1. writeFile(String FileName, String data)
  2. writeFile(String fileName, String encoding, String content)
  3. writeFile(String fileName, String fileContent)
  4. writeFile(String fileName, String fileData)
  5. writeFile(String fileName, String message)
  6. writeFile(String filename, String s)
  7. writeFile(String fileName, String source)
  8. writeFile(String filename, String str)
  9. writeFile(String filename, String string)