Here you can find the source of writeFile(File file, Iterable
Parameter | Description |
---|---|
file | a parameter |
lines | a parameter |
Parameter | Description |
---|---|
IOException | an exception |
public static void writeFile(File file, Iterable<String> lines) throws IOException
//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(); } }