Java:如何读取名称中包含日期的文件

Java:如何读取名称中包含日期的文件,java,Java,我需要使用java读取一个名为C:\Logs\file.Api.yyyyMMddHHmm.txt的文件 如何让java读取file.Api.yyyyMMddHHmm.txt我正忙于构建一个日志跟踪应用程序,我面临的挑战之一是如何获取文件名,其中的文件名将是今天的日期文件.Api.yyymmddhhmm.txt public class BuTailer implements Runnable { // private BuHostManager HostManager = new

我需要使用java读取一个名为C:\Logs\file.Api.yyyyMMddHHmm.txt的文件

如何让java读取file.Api.yyyyMMddHHmm.txt我正忙于构建一个日志跟踪应用程序,我面临的挑战之一是如何获取文件名,其中的文件名将是今天的日期文件.Api.yyymmddhhmm.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;
    }
}
公共类BuTailer实现可运行{
//
private BuHostManager HostManager=new BuHostManager();
私有BuJsonManager JsonManager=new BuJsonManager();
私有BuRestSender RestSender=新BuRestSender();
//
私有字符串文件名;
// 
私人int collectionInterval=30000;
//
private volatile boolean stopThread=false;
//
私有文件文件跟踪;
//
私有long lastnownposition=0;
//
专用BuTailer(字符串文件名){
这(文件名,30000);
}
//
公共BuTailer(字符串文件名,int collectionInterval){
//
this.fileName=文件名;
this.collectionInterval=collectionInterval;
this.fileToWatch=新文件(文件名);
}
//
@凌驾
公开募捐{
//
如果(!fileToWatch.exists()){
//
抛出新的IllegalArgumentException(文件名+“未找到”);
}
//
试一试{
//
而(!stopThread){
//
线程。睡眠(collectionInterval);
long fileLength=fileToWatch.length();
//
if(文件长度lastnownposition){
//
RandomAccessFile RandomAccessFile=新的RandomAccessFile(fileToWatch,“r”);
randomAccessFile.seek(lastKnownPosition);
//
字符串行=null;
//
而((line=randomAccessFile.readLine())!=null){
//TODO:通过REST将消息发布到Netcool或Dynatrace
//System.out.println(JsonManager.BuConvertLogEventToJson(HostManager.BuGetHostName(),this.fileName,line));
BuPost(JsonManager.BuConvertLogEventToJson(HostManager.BuGetHostName(),this.fileName,line));
}
//
lastKnownPosition=randomAccessFile.getFilePointer();
randomAccessFile.close();
}
}
}捕获(例外e){
//
e、 printStackTrace();
停止运行();
}
}
//
公共布尔值isStopThread(){
返回止动螺纹;
}
//
公共void setStopThread(布尔停止线程){
//
this.stopThread=stopThread;
}
//
public void stop running(){
stopThread=false;
}
}
…我面临的挑战是能否获取文件名,其中的文件名将是todays date file.Api.yyyyMMddHHmm.txt

在你想要/期望的任何时区获得今天的日期。时区至关重要。在任何一个特定的时刻,世界各地的日期都因地区而异。这是日本东京的“明天”,而美国俄亥俄州托莱多的“昨天”

ZoneId z = ZoneId.of( "America/Edmonton" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
以所需格式生成表示该时刻的文本

DateTimeFormatter f = DateTimeFormatter.ofPattern( "uuuuMMddHHmm" ) ;
String s = zdt.format( f ) ;
与预期文件名的其他部分合并

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

我很困惑。是否要读取文件的内容,还是要使用今天的日期搜索文件然后读取?是否在中查看了java.text.simpleDataFormat示例如果您知道日期(例如今天或昨天),请使用
simpleDataFormat
获取文件名的该部分。如果没有,请使用
File.listFiles()
列出目录的内容并搜索所需的文件(例如最新的文件)。@EmreSevinç。