Here you can find the source of writeStringToFile(@Nonnull String string, @Nonnull final File file)
Parameter | Description |
---|---|
string | String to write |
file | file to write to |
Parameter | Description |
---|---|
IOException | If an I/O error occurs |
public static void writeStringToFile(@Nonnull String string, @Nonnull final File file) throws IOException
//package com.java2s; /* Copyright (c) 2013 dumptruckman * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import static com.google.common.base.Preconditions.checkNotNull; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import javax.annotation.Nonnull; public class Main { /**// ww w . j a v a 2 s . c om * Writes a {@link String} to a {@link File}. * * @param string String to write * @param file file to write to * * @throws IOException If an I/O error occurs */ public static void writeStringToFile(@Nonnull String string, @Nonnull final File file) throws IOException { checkNotNull(string, "string cannot be null."); checkNotNull(file, "file cannot be null."); try (OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")) { string = string.replaceAll("\n", System.getProperty("line.separator")); out.write(string); } } }