Here you can find the source of writeStringToFile(File outf, String str)
Parameter | Description |
---|---|
outf | the file we are creating or overwriting. |
str | the string we are writing out |
Parameter | Description |
---|---|
FileNotFoundException | if the file can't be opened |
IOException | if there is trouble writing |
public static void writeStringToFile(File outf, String str) throws FileNotFoundException, IOException
//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; } }