-1

i have the below files in the folder

  • 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

i'm getting the filename='DLY-20150721-BOOST_UVERSE-ADJ.xls' from a source. so by refering the file name i want to pick the associated .txt file names like

  • DLY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT.txt
  • DLY-20150721-BOOST_UVERSE-ADJ-ERR.txt

and store it in a buffer. but i'm not getting any idea how to do. i hope by using a regex i guess i can pic the files. but how to pic the whole file names and store in buffer ?

S.Sharath
  • 1
  • 1
  • 1
    So, what's the rule? You remove the .xls extension (which results in "DLY-20150721-BOOST_UVERSE-ADJ"), and pick all the file whose name that start with that string and end with .txt? If that's the rule, then do just that. String.substring(), String.startsWith() and File.listFiles() are your friends. – JB Nizet Sep 05 '15 at 14:19
  • some times i might have the files like * DLY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT-RR20150721181623+0530.txt which i dont want to pick because it is having 'RR' in the file name – S.Sharath Sep 05 '15 at 14:24

1 Answers1

-1

I am a beginner. I don't know how to store files in a buffer. However, to find files using 'DLY-20150721-BOOST_UVERSE-ADJ.xls', try:

  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);
}
Wololo
  • 841
  • 8
  • 20
  • I have edited the question, i want to string used in getNmae().matches("") which can pick the files i wanted not the files having 'RR' in the file names – S.Sharath Sep 05 '15 at 14:59
  • the regex used should pick DLY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT.txt DLY-20150721-BOOST_UVERSE-ADJ-ERR.txt but should not pick the below files. DLY-20150721-BOOST_UVERSE-ADJ-BOOST-DISCONNECT-RR20150721181623+0530.txt DLY-20150721-BOOST_UVERSE-ADJ-ERR-RR20150721181623+0530.txt your logic is picking the above four files – S.Sharath Sep 05 '15 at 15:06
  • I've made edits. I hope it works... – Wololo Sep 05 '15 at 15:17
  • Not working but i have made some edits and now its working. But thanks for the whole idea – S.Sharath Sep 05 '15 at 15:23