简体   繁体   English

如何在 Java 中使用 SFTP 获取目录的最新文件?

[英]How to get the newest file of a directory using SFTP in Java?

I am trying to get the newest file of a directory using SFTP.我正在尝试使用 SFTP 获取目录的最新文件。 The below code gives the correct newest file when there is only one file in the directory.当目录中只有一个文件时,下面的代码给出了正确的最新文件。 If a new file is created in the directory after some time, If I run the below code again its not giving correct newest file, its returning the same old file.(to run the below code I am using timer Scheduler).如果一段时间后在目录中创建了一个新文件,如果我再次运行下面的代码,它没有给出正确的最新文件,它返回相同的旧文件。(运行下面的代码我正在使用计时器调度程序)。

//to have List of all the files of particular directory
List<File> files1 = new ArrayList<File>();  
Vector<LsEntry> files = sftpChannel.ls(filePath+"*.csv");

for (LsEntry entry : files)
  {
      if (!entry.getFilename().equals(".") && !entry.getFilename().equals(".."))
      {

       File f=new File(entry.getFilename());
      files1.add(f);

      }
  }  

System.out.println("files length "+files1.size());
File[] files2=files1.toArray(new File[files1.size()]);  
long lastMod = Long.MIN_VALUE;
File choice = null;
for (File file : files2) {
    if (file.lastModified() > lastMod) {
        choice = file;
        lastMod = file.lastModified();
    }
}
lastModifiedFile=choice;

I even tried using below code.我什至尝试使用下面的代码。 It is also not giving the correct newest file.它也没有提供正确的最新文件。

if (files2.length > 0) {
    //** The newest file comes first 
    Arrays.sort(files2, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
    lastModifiedFile = files2[0];
}

It could be done with Collections or stream since Vector.class is fully competible with Java-Collections:它可以用 Collections 或 stream 来完成,因为 Vector.class 与 Java-Collections 完全兼容:

Vector<LsEntry> list = channelSftp.ls(filePath + "*.csv");
ChannelSftp.LsEntry lastModifiedEntry = Collections.max(list,
    (Comparator.comparingInt(entry-> entry.getAttrs().getMTime()))
);

Or或者

LsEntry lastModifiedEntry = list.stream().max(
    Comparator.comparingInt(entry -> entry.getAttrs().getMTime())
).get();

Use LsEntry.getAttrs().getMTime() to query modification time of a file on SFTP server.使用LsEntry.getAttrs().getMTime()查询 SFTP 服务器上文件的修改时间。

Vector<LsEntry> files = channelSftp.ls(filePath + "*.csv");
LsEntry newestEntry = null;
for (LsEntry entry : files)
{
    if (!entry.getFilename().equals(".") && !entry.getFilename().equals(".."))
    {
        if ((newestEntry == null) ||
            (newestEntry.getAttrs().getMTime() < entry.getAttrs().getMTime()))
        {
            newestEntry = entry;
        }
    }
}

if (newestEntry != null)
{
    System.out.println(
        "Newest file is " + newestEntry.getFilename() +
        " with timestamp " + newestEntry.getAttrs().getMtimeString());
}

To explain why your code does not work: You copy only file names to your File list, so the file.lastModified() cannot return any relevant value.解释为什么您的代码不起作用:您只将文件名复制到File列表中,因此file.lastModified()无法返回任何相关值。 Moreover File object is designed to work with local files only.此外File对象旨在仅与本地文件一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM