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

Sending Jboss Server Logs to Logstash Using Filebeat with Multiline Support

In addition to sending system logs to logstash, it is possible to add a prospector section to the filebeat.yml for jboss server logs. Sometimes jboss server.log has single events made up from several lines of messages. In such cases Filebeat should be configured for a multiline prospector. Filebeat takes lines do not start with a date pattern (look at pattern in the multiline section "^[[:digit:]]{4}-[[:digit:]]{2}-[[:digit:]]{2}" and negate section is set to true ) and combines them with the previous line that starts with a date pattern. server.log file excerpt where DatePattern: yyyy-MM-dd-HH and ConversionPattern: %d %-5p [%c] %m%n Logstash filter: