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

Creating Multiple VLANs over Bonding Interfaces with Proper Routing on a Centos Linux Host

In this post, I am going to explain configuring multiple VLANs on a bond interface. First and foremost, I would like to describe the environment and give details of the infrastructure. The server has 4 Ethernet links to a layer 3 switch with names: enp3s0f0, enp3s0f1, enp4s0f0, enp4s0f1 There are two bond interfaces both configured as active-backup bond0, bond1 enp4s0f0 and enp4s0f1 interfaces are bonded as bond0. Bond0 is for making ssh connections and management only so corresponding switch ports are not configured in trunk mode. enp3s0f0 and enp3s0f1 interfaces are bonded as bond1. Bond1 is for data and corresponding switch ports are configured in trunk mode. Bond0 is the default gateway for the server and has IP address 10.1.10.11 Bond1 has three subinterfaces with VLAN 4, 36, 41. IP addresses are 10.1.3.11, 10.1.35.11, 10.1.40.11 respectively. Proper communication with other servers on the network we should use routing tables. There are three

3 Node (Master Slave Slave) Redis Cluster with Sentinel

It is possible to make your Redis cluster Fault Tolerant and Highly Available by building a replica set and then monitor these nodes using sentinel for automatic failover. I am going to give an example setup to explain it. The structure is built with three nodes running one as a master and two as slaves. Master Node: (Centos 7.2) 192.168.1.11 Slave1 Node: (Centos 7.2) 192.168.1.12 Slave2 Node: (Centos 7.2) 192.168.1.13 Edit System settings on each node: /etc/sysctl.conf Disable transparent hugepage (transparent_hugepage=never) on each node: /etc/default/grub Apply grub config and reboot each node: Master Node: /etc/redis/6379.conf Slave1 Node: /etc/redis/6379.conf Slave2 Node: /etc/redis/6379.conf Master Node: /etc/redis/sentinel.conf Slave1 Node: /etc/redis/sentinel.conf Slave2 Node: /etc/redis/sentinel.conf Each Node: /etc/systemd/system/multi-user.target.wants/redis-server.service Each Node: /etc/