Java Text File Write writeStringToFile(File file, String str, boolean compress)

Here you can find the source of writeStringToFile(File file, String str, boolean compress)

Description

Write the given String to the File.

License

Apache License

Parameter

Parameter Description
file The file to write to.
str The value to write.

Exception

Parameter Description
IOException This should never happen because PrintWriter willcatch it.

Declaration

public static void writeStringToFile(File file, String str, boolean compress) throws IOException 

Method Source Code

//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();

    }
}

Related

  1. writeStringToFile(File file, String data, String encoding)
  2. writeStringToFile(File file, String data, String encoding)
  3. writeStringToFile(File file, String data, String encoding)
  4. writeStringToFile(File file, String data, String encoding)
  5. writeStringToFile(File file, String s)
  6. writeStringToFile(File file, String string)
  7. writeStringToFile(File file, String string, String encoding)
  8. writeStringToFile(File file, String stringToWrite)
  9. writeStringToFile(File file, String text)