Here you can find the source of saveStringToFile(String toSave, String fn)
Parameter | Description |
---|---|
toSave | String the string to save |
fn | String the file to save the string to |
Parameter | Description |
---|---|
IOException | an exception |
FileNotFoundException | an exception |
public static void saveStringToFile(String toSave, String fn) throws IOException, FileNotFoundException
//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(); } }