Save string value to a file : PrintStream « File Input Output « Java






Save string value to a file

  

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class Main {
  public static void main(String[] argv) {
    saveFile("c:\\a.txt", "value");
  }

  public static void saveFile(final String fileName, final String str) {
    try {
      File f = new File(fileName);
      if (new File(f.getParent()).exists() == false) {
        f.getParentFile().mkdirs();
      }
      f.createNewFile();
      PrintStream p = new PrintStream(new FileOutputStream(f, false));
      p.println(str);
      p.close();

    } catch (Exception e) {
      e.printStackTrace();
      System.err.println(fileName);
    }
  }
}

   
    
  








Related examples in the same category

1.Create print stream for error logger
2.Append string to a text file