Here you can find the source of writeFile(String pathname, String data)
Parameter | Description |
---|---|
pathname | the path to the file. |
data | the string to write. |
Parameter | Description |
---|---|
IOException | an exception |
public static void writeFile(String pathname, String data) throws IOException
//package com.java2s; /* Copyright ? 2015 Gerald Rosenberg. * Use of this source code is governed by a BSD-style * license that can be found in the License.md file. *//*from w w w .j av a 2 s .c o m*/ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.util.HashSet; import java.util.Set; public class Main { /** * Writes a string to the specified file using the default encoding. * * If the file path doesn't exist, it's created. If the file exists, it is overwritten. * * @param pathname the path to the file. * @param data the string to write. * @throws IOException */ public static void writeFile(String pathname, String data) throws IOException { write(new File(pathname), data, false); } public static void write(File file, String data, boolean append) throws IOException { Set<OpenOption> options = new HashSet<OpenOption>(); options.add(StandardOpenOption.CREATE); options.add(StandardOpenOption.WRITE); if (append) { options.add(StandardOpenOption.APPEND); } else { options.add(StandardOpenOption.TRUNCATE_EXISTING); } write(file, data, options.toArray(new OpenOption[options.size()])); } private static void write(File file, String data, OpenOption... options) throws IOException { Path path = file.toPath(); Files.write(path, data.getBytes(), options); } }