Showing posts with label File Handling. Show all posts
Showing posts with label File Handling. Show all posts

Thursday, 16 May 2013

File Handling Using Java

Create Class File.Java
import FILE IO.*;

Example Code for Creating Folder



 // Report is the name of Folder Witch You want to create
    File path = new File("E:\\Report");
   System.out.println("path=="+path);

// Check Folder Is available Or Not  in given Directory

                     File file2 = new File("E:\\Report");
                    if (!file2.exists()) {
                    file2.mkdir();
                    }
//If Exist then Create Directory in file name and copy File into directory


if (file2.exists())
{
      File file1 = new File("E:\\Report\\"+filename+"\\"+filename);
                if (file1.mkdir()) {
                System.out.println("File Name Directory is created!");
                File uploadedFile = new File(file1 + "/" + filename);
                item.write(uploadedFile);
               
                } else {
                System.out.println("Failed to create directory!");
                }
      }





Delete Empty Directory
use This Code
File directory = new File("E:\\Report\\"+filename);
directory.delete();

//If the directory contain File then use this code


File fin = new File("E:\\Filename");
   System.out.println("fin-"+fin);

   for (File file : fin.listFiles()) {
       try {
FileDeleteStrategy.FORCE.delete(file);
System.out.println("delete");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
      // Delete when directory is empty
              fin.delete();
   }