Here you can find the source of writeStringToFile(File file, String str, boolean compress)
Parameter | Description |
---|---|
file | The file to write to. |
str | The value to write. |
Parameter | Description |
---|---|
IOException | This should never happen because PrintWriter willcatch it. |
public static void writeStringToFile(File file, String str, boolean compress) throws IOException
//package com.java2s; /*/*from www. ja va2 s .c om*/ * Copyright 2006 - Gary Bentley * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.File; import java.io.IOException; import java.util.zip.GZIPOutputStream; public class Main { /** * Write the given String to the File. * * @param file The file to write to. * @param str The value to write. * @throws IOException This should never happen because PrintWriter will * catch it. */ public static void writeStringToFile(File file, String str, boolean compress) throws IOException { OutputStream out = new BufferedOutputStream(new FileOutputStream(file)); if (compress) { out = new GZIPOutputStream(out); } PrintWriter pout = new PrintWriter(out); pout.print(str); pout.flush(); pout.close(); } }