Saves a string to a text file - Java java.io

Java examples for java.io:BufferedWriter

Description

Saves a string to a text file

Demo Code

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
  /**************************************************************************
   * Saves a string to a text file/*from w  w  w.ja  v  a  2 s.  c o  m*/
   **************************************************************************/
  static public void string2File(File file, String data) throws FileNotFoundException, IOException {
    FileWriter thisFile = new FileWriter(file);
    BufferedWriter thisFileBuffer = new BufferedWriter(thisFile);

    thisFileBuffer.write(data);
    thisFileBuffer.close();

  }

  /**************************************************************************
   * Saves a string to a text file
   **************************************************************************/
  static public void string2File(String fileName, String data) throws FileNotFoundException, IOException {
    string2File(new File(fileName), data);
  }
}

Related Tutorials