Thursday, 21 February 2013

Creating a Directory using Java

It is often a good idea to have a directory to store your application's data .
Here you can see how you can build your application to create the directory for you.

=================JAVA***CODE====================

//Let's do the import first....

import java.io.File;


public class CreateADirectory  {
public static void main(String args[]) {




// Specify the directory's name and location
File ourDirectory = new File("C://Test ");
      try{




    //
Create the Directory if not exists     
  
            if(!ourDirectory.exists()){
                    ourDirectory.mkdir();   

               }
   
//Catch exception if any
  }catch (Exception e){
  System.err.println("Error: " + e.getMessage());
         }


}
}
// when this program runs it creates the Directory Test 

Wednesday, 20 February 2013

How to delete a file with Java

If we want to delete the file located in C:\Test\file.txt
we need this code :
// Import the following in your code...

import java.io.File;

public class FileDeleter  {
public static void main(String args[]) {

File ourFile = new File("C://Test//file.txt ");
      try{
    // Delete the file if exists     
  
            if(ourFile.exists()){
                    ourFile.delete();    

               }
    

  }catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
         }


}
}

when you run this program it deletes the file.txt