File apps example

Click here to download eclipse supported ZIP file



This is AppendDateToFileName.java file having the source code to execute business logic.



 

    
/**
 
 */
package com.cv.java.file;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 @author Chandra Vardhan
 *
 */
public class AppendDateToFileName {

  /**
   @param args
   */
  public static void main(String[] args) {

    String name = "test log file_take.xlsx";
    String name2 = "test_log.file_take.csv";
    String name3 = "test_log.file_take.xls";
    String name4 = "test_log.file_take.txt";
    String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
    dateStr = dateStr.replaceAll(":""_");
    dateStr = dateStr.replaceAll("-""_");
    dateStr = dateStr.replaceAll("\\.""_");
    
    System.out.println(modifyFileName(name, dateStr));
    System.out.println(modifyFileName(name2, dateStr));
    System.out.println(modifyFileName(name3, dateStr));
    System.out.println(modifyFileName(name4, dateStr));
  }

  private static String modifyFileName(String name, String dateStr) {
    String perparedFileName = name;
    if (name != null) {
      int lastIndexOfDot = name.lastIndexOf(".");
      String firstPart = name.substring(0, lastIndexOfDot);
      String secondPart = name.substring(lastIndexOfDot + 1, name.length());
      if (firstPart != null && secondPart != null) {
        perparedFileName = firstPart + "_" + dateStr + "." + secondPart;
      }
    }
    return perparedFileName;
  }

}


This is FilesModifiedLastFewDays.java file having the source code to execute business logic.



 

    
/**
 
 */
package com.cv.java.file;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 @author Chandra Vardhan
 *
 */
public class FilesModifiedLastFewDays {

  private List<String> resultFileList = new ArrayList<String>();

  /**
   @param args
   */
  public static void main(String[] args) {
    Date currentDate = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(currentDate);
    Scanner scanner = null;
    Scanner scanner2 = null;
    try {
      System.out.println("Please enter how many days files looking for : ");
      scanner = new Scanner(System.in);
      int days = scanner.nextInt();
      System.out.println("Please enter folder to fetch last " + days + "  files : ");
      scanner2 = new Scanner(System.in);
      String path = scanner2.nextLine();
      cal.add(Calendar.DAY_OF_MONTH, -days);
      Date alertDate = cal.getTime();
      FilesModifiedLastFewDays files = new FilesModifiedLastFewDays();
      File directory = new File(path);
      List<String> readFiles = files.readFiles(directory, alertDate);
      System.out.println(readFiles.size());
    catch (Exception e) {

    finally {

      if (scanner != null) {
        scanner.close();
      }
      if (scanner2 != null) {
        scanner2.close();
      }
    }

  }

  public List<String> readFiles(final File directory, Date lastDays) {

    try {
      if (directory != null) {
        File[] listFiles = directory.listFiles();

        if (listFiles != null) {
          for (File file : listFiles) {
            if (file != null) {
              if (file.isDirectory()) {
                readFiles(file, lastDays);
              else {
                boolean lastModified = getLastModified(file, lastDays);
                if (lastModified) {
                  resultFileList.add(file.getAbsolutePath());
                }
              }

            }

          }
        }
      }
    catch (Exception e) {
      e.printStackTrace();
    }
    return resultFileList;
  }

  public static boolean getLastModified(final File dir, final Date lastDaysthrows IOException {
    Date modifiedDate = null;
    if (dir != null && dir.isFile()) {
      modifiedDate = new Date(dir.lastModified());
    }
    if (modifiedDate != null && modifiedDate.after(lastDays)) {
      return true;
    else {
      return false;
    }
  }
}


This is RenameFile.java file having the source code to execute business logic.



 

    
/**
 
 */
package com.cv.java.file;

import java.io.File;

/**
 @author Chandra Vardhan
 *
 */
public class RenameFile {

  /**
   @param args
   */
  public static void main(String[] args) {

    File file = new File("hello.xml");

    try {
      if (!file.exists()) {
        file.createNewFile();
      }
      File newfile = new File("changedHello.xml");
      
      if (file.renameTo(newfile)) {
        System.out.println("I am changed");
      else {
        System.out.println("I didn't change");
      }
    catch (Exception e) {
      System.out.println(e.getMessage());
    }

  }

}


This is pom.xml file having the entries of dependency jars and information to build the application .



	
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.cv.file</groupId> <artifactId>RenameFile</artifactId> <version>1.0</version> </project>


This is log4j.properties file having the entries for logging the information into the console/file.




#By default enabling Console appender
# Root logger option
log4j.rootLogger=INFO, stdout

# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%-5p [%c]:%L -->> %m%n

# Redirect log messages to a log file
#log4j.appender.file=org.apache.log4j.RollingFileAppender
#log4j.appender.file.File=C:\servlet-application.log
#log4j.appender.file.MaxFileSize=5MB
#log4j.appender.file.MaxBackupIndex=10
#log4j.appender.file.layout=org.apache.log4j.PatternLayout
#log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

No comments:

Post a Comment