Expand|Select|Wrap|Line Numbers
- package javaTesting;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- public class FileMove {
- FileInputStream in = null;
- FileOutputStream out = null;
- String newFileName ="";
- File fileout = null;
- public static void main(String[] args) throws IOException {
- try {
- FileMove fileMove = new FileMove();
- fileMove.deleteAction();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public void deleteAction() {
- try {
- File file = new File("D:/temp");
- String monthday = getMOnthDay();
- String outFileName = "D:/temp1/"+monthday;
- createFolderIfNotExist(outFileName);
- copyFiles(file,outFileName);
- file = null;
- System.out.println("iam here");
- deleteAllfiles();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private String getMOnthDay() {
- String dayMonth ="";
- Calendar ca1 = Calendar.getInstance();
- int iDay=ca1.get(Calendar.DATE);
- Date date = new Date();
- SimpleDateFormat sdf;
- sdf = new SimpleDateFormat("MMM");
- System.out.println("Month " + sdf.format(date));
- dayMonth = sdf.format(date)+String.valueOf(iDay);
- System.out.println(dayMonth);
- return dayMonth;
- }
- private void copyFiles(File file, String outFileName) {
- try {
- File[] files = file.listFiles();
- for (int i = 0; i < files.length; i++) {
- newFileName = outFileName+"/"+files[i].getName();
- fileout = new File(newFileName);
- out = new FileOutputStream(fileout);
- in = new FileInputStream(files[i]);
- byte[] bs = new byte[1024];
- int len;
- while ((len = in.read(bs)) > 0) {
- out.write(bs, 0, len);
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- finally
- {
- try
- {
- if(in!=null)
- {
- in.close();
- }
- if(out!=null)
- {
- out.flush();
- out.close();
- }
- }
- catch (Exception e)
- {
- System.out.println("==== Ex pppp "+e);
- e.printStackTrace();
- }
- }
- }
- private void deleteAllfiles() {
- try {
- File file = new File("D:/temp");
- File[] deletefiles = file.listFiles();
- for (int i = 0; i < deletefiles.length; i++) {
- System.out.println(deletefiles[i].delete());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void createFolderIfNotExist(String outFileName) {
- try {
- File createFolder = new File(outFileName);
- if (createFolder.exists()) {
- createFolder.mkdir();
- System.out.println("exit");
- }
- else {
- System.out.println("not exist");
- createFolder.mkdir();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }