Java Write String to File writeFile(File file, Iterable lines)

Here you can find the source of writeFile(File file, Iterable lines)

Description

Write a bunch of Strings to a file, overwriting anything that was on it

License

Open Source License

Parameter

Parameter Description
file a parameter
lines a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void writeFile(File file, Iterable<String> lines) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    /**//from  w  w  w .j a v a  2s.  c o  m
     * Write a bunch of Strings to a file, overwriting anything that was on it
     * 
     * @param file
     * @param lines
     * @throws IOException
     */
    public static void writeFile(File file, Iterable<String> lines) throws IOException {
        if (file == null)
            throw new IOException("got null file");
        if (file.isDirectory())
            throw new IOException(file + " is a directory");

        file.createNewFile();
        PrintWriter pw = new PrintWriter(file, "UTF8");
        try {
            for (String line : lines)
                pw.println(line);
        } finally {
            pw.close();
        }
    }

    /**
     * overwrite the given file with the given contents. 
     * @param file
     * @param contents
     * @throws IOException
     */
    public static void writeFile(File file, String contents) throws IOException {
        if (file == null)
            throw new IOException("got null file");
        if (file.isDirectory())
            throw new IOException(file + " is a directory");

        FileWriter writer = new FileWriter(file);
        writer.write(contents);
        writer.close();
    }
}

Related

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