How to create a directory and a floder inside the directory and how to copy the files from source to target directory?
>> Tuesday, May 31, 2011
For example we had a floder D;//drafts where there are csv files. and a floder D://pdffiles it consists of some pdf files.
The first challenge is to create a directory like D://archieve/currentmonthandyear/drafts/csv files.
the second challenge is to create a directory like D://archieve/currentmonthandyear/drafts/pdf files.
For this we need a property files assume me.properties
property file consists of key value pairs as shown below
savefile=D:/Drafts/
pdffiles=D:/Payslips/
archivefiles=D:/archive/
archivedrafts=/Drafts/
archivepdffiles=/Payslips/
csvfile=D:/Drafts/payslip.csv
We need a class to call kets from property files ,Take user defined classes and write the following code,
JPropertyReader .java
ResourcebBundle .java is an abstarct class and it is available in java.utill.* package
Take a class to create dirctory and store files in it and delete files.
The first challenge is to create a directory like D://archieve/currentmonthandyear/drafts/csv files.
the second challenge is to create a directory like D://archieve/currentmonthandyear/drafts/pdf files.
For this we need a property files assume me.properties
property file consists of key value pairs as shown below
savefile=D:/Drafts/
pdffiles=D:/Payslips/
archivefiles=D:/archive/
archivedrafts=/Drafts/
archivepdffiles=/Payslips/
csvfile=D:/Drafts/payslip.csv
We need a class to call kets from property files ,Take user defined classes and write the following code,
JPropertyReader .java
import java.util.ResourceBundle;
public class JPropertyReader {
public static String readProperty(String key) {
String PROPERTY_FILE=MessageConstants.prop_dir;
ResourceBundle rb = ResourceBundle.getBundle(PROPERTY_FILE);
String propvalue=rb.getString(key);
return propvalue;
}
public static String readlog4JProperty(String strKey)
{ String PROPERTY_NAME="ATR";
ResourceBundle rb=ResourceBundle.getBundle(PROPERTY_NAME);
String strPropertyValue=rb.getString(strKey);
return strPropertyValue;
}
}
ResourcebBundle .java is an abstarct class and it is available in java.utill.* package
Take a class to create dirctory and store files in it and delete files.
package com.centris.atr.common.util;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import org.apache.commons.io.FileUtils;
public class FileOperations {
public void copy_Directory_Files(){
System.out.println("----u r in copy_directory==========");
String[] monthName = {"January", "February",
"March", "April", "May", "June", "July",
"August", "September", "October", "November",
"December"};
Calendar cal = Calendar.getInstance();
String monthSalary = monthName[(cal.get(Calendar.MONTH)-1)]; //current month-1 means pervious month
int yearSalary = cal.get(Calendar.YEAR);//current year
String savefile=JPropertyReader.readProperty("savefile"); D://drafts
File srcDir = new File(savefile);
File srcDir1 = new File(JPropertyReader.readProperty("pdffiles"));//D://pdffiles
String archivedraftfiles=JPropertyReader.readProperty("archivefiles")+monthSalary+yearSalary+JPropertyReader.readProperty("archivedrafts");//here we will create directory like D://archieve/perviousmonth with year/drafts
String archievepdffiles=JPropertyReader.readProperty("archivefiles")+monthSalary+yearSalary+JPropertyReader.readProperty("archivepdffiles"); //D://archieve/perviousmonth with year/pdffiles
File trgDir = new File(archivedraftfiles);
File trgDir1 = new File(archievepdffiles);
//boolean success=true;
try {
FileUtils.copyDirectory(srcDir, trgDir);//copying files from source to target
FileUtils.copyDirectory(srcDir1, trgDir1);
} catch (IOException e) {
e.printStackTrace();
}
}
public static boolean delete_Files(){
boolean t=true;
String savefile=JPropertyReader.readProperty("savefile");
String pdffiles=JPropertyReader.readProperty("pdffiles");
File directory = new File(savefile);
File[] files = directory.listFiles();//deleting the files csvfile from d://drafts/csvfile
for (File file : files) {
if (!file.delete())
{
t=false;
}
}
File directory1 = new File(pdffiles);
File[] files1 = directory1.listFiles();
for (File file : files1) {
if (!file.delete())
{
t=false;
}
}
return t;
}
public static void main(String args[]){
FileOperations objFileOperations=new FileOperations();
objFileOperations.copy_Directory_Files();
}
0 comments:
Post a Comment