Java Text File Write writeStringToFile(File outf, String str)

Here you can find the source of writeStringToFile(File outf, String str)

Description

Write a String to a file.

License

Open Source License

Parameter

Parameter Description
outf the file we are creating or overwriting.
str the string we are writing out

Exception

Parameter Description
FileNotFoundException if the file can't be opened
IOException if there is trouble writing

Declaration

public static void writeStringToFile(File outf, String str) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
import java.io.File;

import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Main {
    /**/* w w w  .j av a 2s . co m*/
    Write a String to a file.
        
    @param filePath the path of the file we are creating or
    overwriting.
    @param str the string we are writing out
    @throws FileNotFoundException if the file can't be opened
    @throws IOException if there is trouble writing
    */
    public static void writeStringToFile(String filePath, String str) throws FileNotFoundException, IOException {
        File outf = new File(filePath);
        writeStringToFile(outf, str);

        return;
    }

    /**
    Write a String to a file.
        
    @param outf the file we are creating or overwriting.
    @param str the string we are writing out
    @throws FileNotFoundException if the file can't be opened
    @throws IOException if there is trouble writing
    */
    public static void writeStringToFile(File outf, String str) throws FileNotFoundException, IOException {
        if (outf == null || str == null) {
            return;
        }

        // make sure the destination directory exists
        File dirf = outf.getParentFile();
        if (dirf != null && !dirf.exists()) {
            // parent directory doesn't exist, create it
            dirf.mkdirs();
        }

        FileOutputStream fout = new FileOutputStream(outf);
        byte[] bb = str.getBytes();
        fout.write(bb);
        fout.close();

        return;
    }
}

Related

  1. writeStringToFile(File file, String stringToWrite)
  2. writeStringToFile(File file, String text)
  3. writeStringToFile(File file, String text)
  4. writeStringToFile(File file, String text, boolean append)
  5. writeStringToFile(File file, String xml)
  6. writeStringToFile(File outFile, String s)
  7. writeStringToFile(File p_out, String p_str)
  8. writeStringToFile(File pFile, String pString)
  9. writeStringToFile(final File file, final String data)