Java OutputStreamWriter Write saveStringToFile(String toSave, String fn)

Here you can find the source of saveStringToFile(String toSave, String fn)

Description

Save the specifed string to the given file name

License

Open Source License

Parameter

Parameter Description
toSave String the string to save
fn String the file to save the string to

Exception

Parameter Description
IOException an exception
FileNotFoundException an exception

Declaration

public static void saveStringToFile(String toSave, String fn) throws IOException, FileNotFoundException 

Method Source Code


//package com.java2s;
// The MIT License

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;

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

public class Main {
    /**/*from w  ww.ja va2 s. c o m*/
     * Save the specifed string to the given file name
     * @param toSave String the string to save
     * @param fn String the file to save the string to
     * @throws IOException
     * @throws FileNotFoundException
     */
    public static void saveStringToFile(String toSave, String fn) throws IOException, FileNotFoundException {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fn)));
        java.util.StringTokenizer st = new java.util.StringTokenizer(toSave, "\n", true);
        String s;
        while (st.hasMoreTokens()) {
            s = st.nextToken();
            if (s.equals("\n"))
                writer.newLine();
            else
                writer.write(s);
        } // while
        writer.flush();
        writer.close();
    }
}

Related

  1. saveLongTxtList(long[] ids, String[] texts, String listFile)
  2. saveString(File f, String enc, String text)
  3. saveString(String string, File file, String charsetName)
  4. saveString2File(String s, String filename)
  5. saveStringToFile(String filename, String string)
  6. saveStringToPath(String stringToSave, String pathToSaveTo)
  7. saveText(BufferedWriter writer, String string)
  8. saveTextInFile(File file, String content, String encoding)
  9. saveTxt(OutputStream os, List list)