2015-09-05 43 views
-1

我有文件夹我怎么可以存在于一个文件,并用java

  • 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

我得到从源文件名= 'DLY-20150721-BOOST_UVERSE-ADJ.xls'。 所以通过它交给文件名我要挑喜欢

  • DLY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT.txt
  • DLY-20150721-BOOST_UVERSE-ADJ-ERR相关.txt文件名。 txt

并将其存储在缓冲区中。但我不知道该怎么做。 我希望通过使用正则表达式我想我可以拍摄这些文件。但如何拍摄整个文件名并存储在缓冲区中?

+1

那么,什么是规则?您删除.xls扩展名(导致“DLY-20150721-BOOST_UVERSE-ADJ”),然后选择所有名称以该字符串开头并以.txt结尾的文件?如果这是规则,那么就这样做。 String.substring(),String.startsWith()和File.listFiles()是你的朋友。 –

+0

有时我可能有文件一样 * DLY-20150721-BOOST_UVERSE-ADJ-BOOST断开,RR20150721181623 + 0530.txt 这我不想接,因为它是有文件名 –

回答

-1

我是初学者。我不知道如何将文件存储在缓冲区中。但是,要使用'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); 
} 
+0

我有“RR”编辑问题,我想在getNmae()。matches(“”)中使用字符串,它可以选择我想要的文件而不是文件名中具有'RR'的文件 –

+0

我不明白你的意思。 – Saud

+0

使用的正则表达式应该选择 DLY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT.txt DLY-20150721-BOOST_UVERSE-ADJ-ERR.txt ,但不应该挑以下文件。 DLY-20150721-BOOST_UVERSE-ADJ-BOOST断开,RR20150721181623 + 0530.txt DLY-20150721-BOOST_UVERSE-ADJ-ERR-RR20150721181623 + 0530.txt 你的逻辑是采摘上述四个文件 –