Java Write String to File writeFile(File f, String str)

Here you can find the source of writeFile(File f, String str)

Description

Writes a string(str) to a File(f) ** THis method overwrites any data in the file

License

Apache License

Declaration

public static void writeFile(File f, String str) throws Exception 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**//from   ww w  . ja v a  2  s .com
     * Writes a string(str) to a File(f) ** THis method overwrites any data in the file
     */
    public static void writeFile(File f, String str) throws Exception {
        if (!f.exists()) {
            if (!f.createNewFile()) {
                throw new Exception("File was not created!");
            }
        }
        if (!f.canWrite())
            throw new Exception("No permission to write file!");
        else {
            try {
                // WE CAN WRITE TO THE FILE
                FileWriter fw = new FileWriter(f, false);
                fw.write(str);
                fw.close();
            } catch (Exception e) {
                throw e;
            }
        }
    }
}

Related

  1. writeFile(File f, String content, String encoding)
  2. writeFile(File f, String contents)
  3. writeFile(File f, String entry)
  4. writeFile(File f, String s)
  5. writeFile(File f, String str)
  6. writeFile(File f, String text)
  7. writeFile(File file, Iterable lines)
  8. writeFile(File file, String content)
  9. writeFile(File file, String content)