2015-09-05 45 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 你的邏輯是採摘上述四個文件 –