Skip to main content

Just another file copy program

There are two files in the project. gates.java and gates.properties. gates.java reads from properties file. In the properties file there are some configuration parameters.

gates.properties example:
sDirParam = /source
dDirParam = /destination
dFileExtParam = $G$
fileFilterParam = .srt;.avi;.pdf;
logFileParam = /logs/gates.log



gates.java:
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.util.Properties;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.text.DateFormat;

public class gates {

static DateFormat df = new SimpleDateFormat ("dd.MM.yyyy  hh:mm:ss");
    public void wrLog(String file, String msg) {
        try {
            Date now = new Date();
            String currentTime = gates.df.format(now);
            FileWriter aWriter = new FileWriter(file, true);
            aWriter.write(currentTime + " " + msg + System.getProperty("line.separator"));
            aWriter.flush();
            aWriter.close();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
    
public String[] getFileList(String dir, String extFilter) {
 
File directory = new File(dir);
final String[] okFileExtensions ;
String delimiter = ";";
okFileExtensions  = extFilter.split(delimiter);
 
if (!directory.isDirectory()) {
System.out.println("No directory provided!!!");
    return null;
  }
 
FilenameFilter filefilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
for (String ext : okFileExtensions) {
if (name.endsWith(ext))
    {
return true;
    }
}
return false;
}
};
String[] filenames = directory.list(filefilter);
return filenames;
}
public static void main(String[] args) throws IOException {
 
Properties props = new Properties();
InputStream in = gates.class.getClassLoader().getResourceAsStream("gates.properties");
props.load(in);
String sDir = props.getProperty("sDirParam");
String dDir = props.getProperty("dDirParam");
String dFileExt = props.getProperty("dFileExtParam");
String allowedExt = props.getProperty("fileFilterParam");
String logFile = props.getProperty("logFileParam");
in.close();
 
gates movePicture = new gates();
movePicture.wrLog(logFile, "gates LOG file: ");
String[] fileList = movePicture.getFileList(sDir,allowedExt);
 
for (String sFileName : fileList) {
int dotPos = sFileName.lastIndexOf(".");
String sFileNameWithoutExt = sFileName.substring(0, dotPos);
String dFileNameTemp = sFileNameWithoutExt + "." + dFileExt;
String dFileName = sFileName;
 
File sFile = new File(sDir + "/" + sFileName);
File dFileTemp = new File(dDir + "/" + dFileNameTemp);
File dFile = new File(dDir + "/" + dFileName);
if(!sFile.exists())
{
movePicture.wrLog(logFile, "Source file does not exist!!! " + sDir + "/" + sFileName);
System.exit(0);
}
 
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sFile), 4096);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dFileTemp), 4096);
int theChar;
          while ((theChar = bis.read()) != -1) {
             bos.write(theChar);
          }
        bos.close();
        bis.close();
        movePicture.wrLog(logFile, "Copy done " + sFileName);
} catch (Exception ex) {
ex.printStackTrace();
}
 
boolean ren = dFileTemp.renameTo(dFile);
if(!ren) {
movePicture.wrLog(logFile, "FileExtension failed to change!!! " + dDir + "/" + dFileNameTemp);
}
else {
movePicture.wrLog(logFile, "FileExtension changed successfully " + dDir + "/" + dFileName);
boolean  del = sFile.delete();
if (!del) {
movePicture.wrLog(logFile, "Source file deletion failed!!! " + sDir + "/" + sFileName);
}
else {
movePicture.wrLog(logFile, "Source file deleted successfully " + sDir + "/" + sFileName);
}
 
}
   
}
}
}

Comments

Popular posts from this blog