Sample Programs

Sample java program to create temporary file

 package org.jusfortechies.example; 

 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
 import java.io.IOException;
 
 public class CreateTemporaryFile {

   public static void main(String[] args) {
     
	 try {

       // Create a temporary file.
       File tempFile = File.createTempFile("filename", ".extension");

       // Delete temporary file when program exits.
       tempFile.deleteOnExit();

       // Write text to temporary file
       BufferedWriter out = new BufferedWriter(new FileWriter(tempFile));
       out.write("This is sample text in temporary file");
       out.close();

     } catch (IOException e) {
       System.err.println("File could not be created : " + e);
     }
	
   }

 }




blog comments powered by Disqus