Thursday, 14 June 2018

Automatically create and delete a file in java programing

If you want create a file in java automatically and delete this file auto then use this code
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javasample;
import java.util.*;
import java.io.*;
/**
 *
 * @author rajesh kumar shukla
 */
public class file extends TimerTask {
 static File file;
     public static void main(String[] args ) throws IOException
         {
         file = new File ("test.dat");
         if (! file.exists() )
             {
             file.createNewFile();
         }
         System.out.println("File Created");
         file test = new file();
         Timer t = new Timer ();
         t.schedule(test, 30*1000L);
         try
             {
             while (file.exists())
                 {
                 System.out.print('.');
                 Thread.sleep(1000);
             }
         }
         catch (InterruptedException ie)
             {
             System.out.println("Error");
         }
         System.exit(0);
     } //end of main
     public void run()
         {
         file.delete();
     }
} //end of public class ab

0 comments:

Post a Comment