Creating a Temporary File - Java File Path IO

Java examples for File Path IO:Temporary File

Description

Creating a Temporary File

Demo Code

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

public class Main {
  public static void main(String[] argv) {
    try {// w  ww  . j  a v a  2s  .c  o  m
      // Create temp file.
      File temp = File.createTempFile("pattern", ".suffix");

      // Delete temp file when program exits.
      temp.deleteOnExit();

      // Write to temp file
      BufferedWriter out = new BufferedWriter(new FileWriter(temp));
      out.write("aString");
      out.close();
    } catch (IOException e) {
    }
  }
}

Related Tutorials