JSch ChannelSftp.ls - 在java中传递匹配模式 [英] JSch ChannelSftp.ls - pass match patterns in java

查看:2836
本文介绍了JSch ChannelSftp.ls - 在java中传递匹配模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sftp位置有多个文件,例如

I have multiple files at an sftp location like

xyz_20140101.csv.gz
xyz_2014_01_01.csv.gz
xyz_20140202.csv.gz
xyz_2014_02_02.csv.gz

通过我的java程序我想获取格式为xyz_YYYYMMDD.csv.gz的文件列表,我应该在ChannelSftp.ls命令中传递匹配模式。

through my java program i want to get list of files only in format xyz_YYYYMMDD.csv.gz , what should be my match pattern to pass in ChannelSftp.ls command .

我正在传递

pattern = xyz_*csv.gz , but it gives me all the files .

ChannelSftp.ls(pattern);

我的模式应该在ls命令中传递什么?

What should be my pattern to pass in ls command ?

推荐答案

ChannelSftp.ls将路径作为参数: http://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/ChannelSftp.html#ls(java。 lang.String)

ChannelSftp.ls takes as argument a path: http://epaul.github.io/jsch-documentation/javadoc/com/jcraft/jsch/ChannelSftp.html#ls(java.lang.String)

路径可以包含glob模式通配符(*或?)但是这样你就无法检查日期中是否有数字。

the path can contain glob pattern wildcards (* or ?) but with this you are not able to check that date has digits in it.

所以只需列出路径并在

        Vector ls = channelSftp.ls(path);
        Pattern pattern = Pattern.compile("xyz_[0-9]{8}.csv.gz");
        for (Object entry : ls) {
            ChannelSftp.LsEntry e = (ChannelSftp.LsEntry) entry;
            //match regex on e.getFilename()
            Matcher m = pattern.matcher(e.getFilename());
            if (m.matches()) {
                //TODO you code
            }

        }

如果您不需要检查日期的格式是数字,您可以使用以下模式和ChannelSftp.ls

in case you don't need to check that date is formatted from digits you can just use following pattern and ChannelSftp.ls

pattern =  xyz_????????.csv.gz

但这也会匹配:xyz_2014_aaa.csv.gz

but this will also match something like: xyz_2014_aaa.csv.gz

这篇关于JSch ChannelSftp.ls - 在java中传递匹配模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆