如何使用java获取文件中存在的文件名并将其存储在缓冲区中

如何使用java获取文件中存在的文件名并将其存储在缓冲区中,java,regex,string,file,Java,Regex,String,File,我在文件夹中有以下文件 DLY-20150721-BOOST_UVERSE-ADJ.xls DLY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT.txt DLY-20150721-BOOST_UVERSE-ADJ-ERR.txt DLY-20150721-BOOST_UVERSE-ADJ-RR20150721181623+0530.xls DLY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT-RR201507211

我在文件夹中有以下文件

  • DLY-20150721-BOOST_UVERSE-ADJ.xls
  • DLY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT.txt
  • DLY-20150721-BOOST_UVERSE-ADJ-ERR.txt
  • DLY-20150721-BOOST_UVERSE-ADJ-RR20150721181623+0530.xls
  • DLY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT-RR20150721181623+0530.txt
  • DLY-20150721-BOOST_UVERSE-ADJ-ERR-RR20150721181623+0530.txt
我从一个来源获得文件名='DY-20150721-BOOST_UVERSE-ADJ.xls'。 因此,通过引用文件名,我想选择相关的.txt文件名,如

  • DLY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT.txt
  • DLY-20150721-BOOST_UVERSE-ADJ-ERR.txt
并将其存储在缓冲区中。但我不知道该怎么做。
我希望通过使用正则表达式,我想我可以拍摄这些文件。但是如何pic整个文件名并存储在缓冲区中呢

我是初学者。我不知道如何在缓冲区中存储文件。但是,要使用“DLY-20150721-BOOST_UVERSE-ADJ.xls”查找文件,请尝试:

  import java.io.File;

public class X {
public static void main(String[] args) {
    File file = new File("FOLDER_PATH_HERE");
    File[] files = file.listFiles();
    String filename1 = "DLY-20150721-BOOST_UVERSE-ADJ.xls"; // File to search for
    String filename2 = removeExtension(filename); // filename without extension
    for(int i = 0; i<files.length; i++) {
        if( files[i].getName().matches(filename2 + ".*") 
                && getExtension(files[i]).equals(".txt")
                && (files[i].getName().indexOf("RR") == -1) ) {
            //Store file in a buffer
        }
    }
}
public static String getExtension(File file) {
    String fileName = file.getName();
    int lastDot = fileName.lastIndexOf('.');
    return fileName.substring(lastDot);
}
}
public static String removeExtension(File file) {
    String fileName = file.getName();
    int lastDot = fileName.lastIndexOf('.');
    return fileName.substring(0,lastDot);
}
导入java.io.File;
公共X类{
公共静态void main(字符串[]args){
File File=新文件(“此处为文件夹路径”);
File[]files=File.listFiles();
字符串filename1=“DLY-20150721-BOOST_UVERSE-ADJ.xls”//要搜索的文件
字符串filename2=removeExtension(filename);//不带扩展名的filename

对于(inti=0;iSo,规则是什么?删除.xls扩展名(结果是“DLY-20150721-BOOST_UVERSE-ADJ”),然后选择名称以该字符串开头并以.txt结尾的所有文件?如果这是规则,则只需执行.string.substring()、string.startsWith()和file.listFiles()是你的朋友。有时我可能会有像*DLY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT-RR20150721181623+0530.txt这样的文件,我不想选择,因为它的文件名中有“RR”,我编辑了这个问题,我想在getNmae()中使用字符串。匹配(“”)它可以选择我想要的文件而不是文件名中有“RR”的文件使用的正则表达式应该选择DY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT.txt DY-20150721-BOOST_UVERSE-ADJ-ERR.txt,但不应该选择以下文件。DY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT-RR20150721181623+0530.txt DY-20150721-BOOST_UVERSE-ADJ-ERR-RR20150721181623+0530.txt您的逻辑是选择上述四个文件。我已经做了编辑。我希望它可以工作…不工作,但我已经做了一些编辑,现在它可以工作了。但是谢谢您的整个想法