Thobs More
Thobs More

Reputation: 63

Java: How to read a file with date as part of the name

I need to read a file with the following name C:\Logs\file.Api.yyyyMMddHHmm.txt using java.

How do I get java to read file.Api.yyyyMMddHHmm.txt I am busy building a log tailing app and one of the challenges I have is being able to get file name where the filename will be todays date file.Api.yyyyMMddHHmm.txt.

public class BuTailer implements Runnable {
    //
    private BuHostManager HostManager = new BuHostManager();
    private BuJsonManager JsonManager = new BuJsonManager();
    private BuRestSender RestSender = new BuRestSender();
    //
    private String fileName;
    // 
    private int collectionInterval = 30000;
    //
    private volatile boolean stopThread = false;
    //
    private File fileToWatch;
    //
    private long lastKnownPosition = 0;
    //
    private BuTailer(String fileName) {
        this(fileName, 30000);
    }
    //
    public BuTailer(String fileName, int collectionInterval) {
        //
        this.fileName = fileName;
        this.collectionInterval = collectionInterval;
        this.fileToWatch = new File(fileName);
    }
    
    //
    @Override
    public void run() {
        //
        if (!fileToWatch.exists()) {
            //
            throw new IllegalArgumentException(fileName + " not found");
        }
        //
        try {
            //
            while(!stopThread) {
                //
                Thread.sleep(collectionInterval);
                long fileLength = fileToWatch.length();
                //
                if (fileLength < lastKnownPosition) {
                    //
                    lastKnownPosition = 0;
                }
                //
                if (fileLength > lastKnownPosition) {
                    //
                    RandomAccessFile randomAccessFile = new RandomAccessFile(fileToWatch, "r");
                    randomAccessFile.seek(lastKnownPosition);
                    //
                    String line = null;
                    //
                    while ((line = randomAccessFile.readLine()) != null) {
                        // TODO: post message via REST to Netcool or Dynatrace
                        // System.out.println( JsonManager.BuConvertLogEventToJson(HostManager.BuGetHostName() , this.fileName, line));
                        RestSender.BuPost(JsonManager.BuConvertLogEventToJson(HostManager.BuGetHostName() , this.fileName, line));
                    }
                    //
                    lastKnownPosition = randomAccessFile.getFilePointer();
                    randomAccessFile.close();
                }
            }
        } catch (Exception e) {
            //
            e.printStackTrace();
            stopRunning();
        }
    }
    //
    public boolean isStopThread() {
        return stopThread;
    }
    //
    public void setStopThread(boolean stopThread) {
        //
        this.stopThread = stopThread;
    }
    //
    public void stopRunning() {
        stopThread = false;
    }
}

Upvotes: 0

Views: 105

Answers (1)

Basil Bourque
Basil Bourque

Reputation: 339043

… challenges I have is being able to get file name where the filename will be todays date file.Api.yyyyMMddHHmm.txt.

Get today’s date as seen in whatever time zone you desire/expect. A time zone is crucial. For any given moment, the date varies around the globe by zone. It is “tomorrow” in Tokyo Japan while still “yesterday” in Toledo Ohio US.

ZoneId z = ZoneId.of( "America/Edmonton" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

Generate text representing that moment in the format you desire.

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmm" ) ;
String s = zdt.format( f ) ;

Combine with the other parts of your expected file name.

String fileName = "file.Api." + s + ".txt"

Upvotes: 1

Related Questions