栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用Java查找SFTP最旧文件的文件大小和最后修改

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用Java查找SFTP最旧文件的文件大小和最后修改

事实证明,这在JSch中是完全可能的,最困难的部分只是查找文档。我在下面使用的代码,希望其他人会有所帮助!(我知道,我知道需要进行优化。在其他地方也定义了变量,但是希望任何需要此变量的人都可以解决这些问题!)

public static String oldestFile() {    Vector list = null;    int currentOldestTime;    int nextTime = 2140000000; //Made very big for future-proofing    ChannelSftp.LsEntry lsEntry = null;    SftpATTRS attrs = null;    String nextName = null;    try {        list = Main.chanSftp.ls("*.xml");        if (list.isEmpty()) { fileFound = false;        }        else { lsEntry = (ChannelSftp.LsEntry) list.firstElement(); oldestFile = lsEntry.getFilename(); attrs = lsEntry.getAttrs(); currentOldestTime = attrs.getMTime(); for (Object sftpFile : list) {     lsEntry = (ChannelSftp.LsEntry) sftpFile;     nextName = lsEntry.getFilename();     attrs = lsEntry.getAttrs();     nextTime = attrs.getMTime();     if (nextTime < currentOldestTime) {         oldestFile = nextName;         currentOldestTime = nextTime;     } } attrs = chanSftp.lstat(Main.oldestFile); long size1 = attrs.getSize(); System.out.println("-Ensuring file is not being written to (waiting 1 minute)"); Thread.sleep(60000); //Wait a minute to make sure the file size isn't changing attrs = chanSftp.lstat(Main.oldestFile); long size2 = attrs.getSize(); if (size1 == size2) {     System.out.println("-It isn't.");     fileFound = true; } else {     System.out.println("-It is.");     fileFound = false; }        }    } catch (Exception ex) {ex.printStackTrace();}    return Main.oldestFile;}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/595862.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号