Java searching file name from the one folder

前端 未结 3 1840
天命终不由人
天命终不由人 2021-01-26 02:04

I am studying Java and I am not really sure the way to searching file. I would like to build the function which returning file names ( the files name should begin with st

3条回答
  •  日久生厌
    2021-01-26 02:12

    // You'll need this import: import java.io.File;
    
    File folder = new File("C:/Folder_Location");
    // gets you the list of files at this folder
    File[] listOfFiles = folder.listFiles();
    // loop through each of the files looking for filenames that match
    for(int i = 0; i < listOfFile.length; i++){
        String filename = listOfFiles[i].getName();
        if(filename.startsWith("Stuff") && listOfFiles[i].getName().endsWith("OtherStuff")){
            // do something with the filename
        }
    }
    

提交回复
热议问题