Java Text File Write writeStringToFile(String s, File f)

Here you can find the source of writeStringToFile(String s, File f)

Description

write String To File

License

Apache License

Declaration

public static void writeStringToFile(String s, File f) throws IOException 

Method Source Code

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

import java.io.File;

import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static void writeStringToFile(String s, File f) throws IOException {
        if (f.exists()) {
            if (!f.delete()) {
                throw new RuntimeException("Cannot delete file " + f.getAbsolutePath());
            }//www.j a  v a2s  .  c  o m
        }

        FileWriter fw = null;
        try {
            if (!f.createNewFile()) {
                throw new RuntimeException("Cannot create new file : " + f.getAbsolutePath());
            }
            fw = new FileWriter(f);

            fw.write(s);
        } catch (IOException e) {
            throw (e);
        } finally {
            if (fw != null) {
                try {
                    fw.close();
                } catch (IOException e) {
                    throw (e);
                }
            }
        }
    }
}

Related

  1. writeStringToFile(String path, String content)
  2. writeStringToFile(String path, String content)
  3. writeStringToFile(String path, String contents)
  4. writeStringToFile(String s, File f)
  5. writeStringToFile(String s, File f)
  6. writeStringToFile(String s, File file)
  7. writeStringToFile(String s, String filename)
  8. writeStringToFile(String s, String filePathname)
  9. writeStringToFile(String sdat, File f)