Thursday, September 27, 2012

Delete All Text Files from Folder using java

import java.io.*;

public class ClearFolderDemo{
 
   public static void clear(String strFolder)
{
        // Declare variables
        File fLogDir = new File(strFolder);
        // Get all BCS files
        File[] fLogs = fLogDir.listFiles(new FilenameFilter()
        {
                public boolean accept(File fDir, String strName)
                {
                        return (strName.endsWith(".txt" ));
                }
        });
        // Delete all files
        for (int i = 0; i < fLogs.length; i++)
        {
             //  deleteFile(fLogs[i].getAbsolutePath());
            if(fLogs[i].getAbsoluteFile().delete()){
                System.out.println(fLogs[i].getAbsoluteFile().getName() + " is deleted!");
            }else{
                System.out.println("Delete operation is failed.");
            }
        }

  public static void main(String[] args){
   String Src="D:\Output";
   clear(Src);
   System.out.println("Deleted All .txt files from Folder");
 
  }
  }
}



Extract Zip File from source to Destination using Java

import java.io.*;
import java.util.*;
import java.util.zip.*;

public class ExtractzipDemo{
 
  public static void extract(String file, String destination) throws IOException {
    ZipInputStream in = null;
    OutputStream out = null;
    try {
      // Open the ZIP file
      in = new ZipInputStream(new FileInputStream(file));
    // Get the first entry
      ZipEntry entry = null;
      while ((entry = in.getNextEntry()) != null) {
        String outFilename = entry.getName();
        // Open the output file
        if (entry.isDirectory()) {
          new File(destination, outFilename).mkdirs();
        } else {
          out = new FileOutputStream(new File(destination,outFilename));
          // Transfer bytes from the ZIP file to the output file
          byte[] buf = new byte[1024];
          int len;
          while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
          }
        // Close the stream
          out.close();
        }
      }
    } finally {
      // Close the stream
      if (in != null) {
        in.close();
      }
      if (out != null) {
        out.close();
      }
    }
  }
 
  public static void main(String[] args){
 
   String Src= "D:\Test\Src.zip";
   String Dest="D:\ZipOutput";
   extract(Src,Dest);
   System.out.println("Done....");
 
  }
  }
}

Copy Directory from Source Folder to Destination Folder Using Java

import java.io.*;

public class Copyexample{
  private static void copyfile(String srFile, String dtFile){
  try{
  File srcPath = new File(srFile);
  File dstPath = new File(dtFile);
      if (srcPath.isDirectory())
        {
            if (!dstPath.exists())
            {
                dstPath.mkdir();
            }

            String files[] = srcPath.list();
            for(int i = 0; i < files.length; i++)
            {
                copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i]));
            }
        }
        else
        {
            if(srcPath.exists())
            {
                InputStream in = new FileInputStream(srcPath);
                OutputStream out = new FileOutputStream(dstPath);
                // Transfer bytes from in to out
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                in.close();
                out.close();
            }
            else
            {
            System.out.println("File or directory does not exist.");
                System.exit(0);
                }
        }
  }catch(FileNotFoundException ex){
  System.out.println(ex.getMessage() + " in the specified directory.");
  System.exit(0);
  }catch(IOException e){
  System.out.println(e.getMessage()); 
  }
  }
  public static void main(String[] args){
 
   String Src= "D:\Test\source";
   String Dest="D:\OutPut";
   copyDirectory(Src,Dest);
   System.out.println("Copied....");
 
  }
  }
}

Monday, September 24, 2012

Convert JPG image to BMP using java

import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class ConvertJPGToBMP
{
 public static void main(String a[]){
  try{ 
       System.out.println("Enter image name\n");
       BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
       String imageName=bf.readLine();
       File input = new File(imageName);
       BufferedImage image = ImageIO.read(input);
       System.out.println("Enter the output image name(.bmp):\n");
       String imageName1=bf.readLine(); 
       File output = new File(imageName1);
       ImageIO.write(image, "bmp", output);
       System.out.println("Your image has been converted successfully");
}catch(FileNotFoundException e){
      System.out.println("Error:"+e.getMessage());
}catch(IOException e){
      System.out.println("Error:"+e.getMessage());
    }catch(Exception e){
    System.out.println(e.getMessage());
  }

  }
}