Sample Application: Auto File

 

This function enables user to writes the given content to a file, view the content and delete it

Users are provided with options to select what they need to do with the file. They can insert a content to the file, view it and if they want they are able to delete file also.

 
// -----------------------------------------------------------------------------
// AutoFile.java
// -----------------------------------------------------------------------------

import java.util.Timer;
import java.util.TimerTask;
import java.io.File;
import java.io.FileOutputStream;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

// TimerTask implements a task that can be scheduled for one-time 
// or repeated execution by a Timer.
public class AutoFile extends TimerTask {

/**
 * ------------------------------------------------------------------------------
 * This function enables user to writes the given content to a file, 
 * view the content and delete it 
 * 
     * File file matches the temporary file to be created 
	 * FileOutputStream generates an output stream for writing data to a 
	 * given file object
	 * BufferedReader bf will buffer the input from the specified file
	 * Timer scales to large numbers of concurrently scheduled tasks
     *          
     * @param filename mapping name of the particular file 
	 * @param content mapping content to be written to the file
	 * @throws IOException
     *
 * ------------------------------------------------------------------------------
**/

    private static File file;

    public static void autoFile(String filename, String content) throws IOException {

		//Creates a new file object
        file = new File(filename);

		//Create a new file if the specified file does not exists
        if (! file.exists() ) {
            file.createNewFile();
        }
        System.out.println("........File Created........");

        AutoFile test = new AutoFile();
       
        FileOutputStream fop=new FileOutputStream(file);
        
        //Writes data to file
        fop.write(content.getBytes());
		//Close output stream
        fop.close();

		//Options available for user
        System.out.println("1.View the content of the file");
        System.out.println("2.View the content of the file and then delete it");
        System.out.println("3.Delete the file");
        
		//Read input from console
        char ch = (char)System.in.read();
       
		//View the file content
        if(ch == '1' || ch == '2') {
            
           BufferedReader bf = new BufferedReader(new FileReader(file));
            
            //Read data from file
            System.out.println( bf.readLine() );
            
            // Close our input stream
            bf.close();
        }
        if(ch == '2' || ch == '3') {
            
            Timer t = new Timer();
            //Schedules the specified task for execution at the specified time
            //Here the time is scheduled for 3 seconds           
            t.schedule(test, 3*1000L);
            try {
                while (file.exists()) {
                    System.out.print('.');
                    //Causes the currently executing thread to sleep
                    Thread.sleep(1000);
                }
            } catch (InterruptedException ie) {
                System.out.println("Error");
            }
        }

        //Terminates the currently running Java Virtual Machine
        System.exit(0);
    } 

    //The action to be performed by this timer task.
    public void run() {

        //Deletes the file
        file.delete();
        System.out.println("\nFile deleted");
    }
} 

 

AutoFile.java writes the given content to a file, view the content and delete it.

 
Contact Us