简体   繁体   中英

output files from the folder with no extension

help make this, the following code finds the txt files in the folder, and how to make it lists the names of files without extensions. Here's the code:

String list[] = new File("C:\\Users\\Eric\\Desktop\\txt\\").list(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return name.endsWith(".txt");
    }
});
for (int i = 0; i < list.length; i++){
    if(myName.equals(list[i])) {
        InputStreamReader reader  = null;
        try{
            File file =  new File("C:\\Users\\Eric\\Desktop\\txt\\" + list[i]);
            reader =  new InputStreamReader(new FileInputStream(file), "UTF-8");

As you can see, right here:

if(myName.equals(list[i]))

Here is the list of files is compared to the value of myname, so could you help give to the comparison sheet and myName and list[i], the file extension was not considered) such as the program displays that folder has the following layout: indeks.html hi.html, user entered indeks.html, compare and good, but how to create one so that the user has entered the code, and the program still has compared it with the list of files in directory?

If I understand correctly, you want to list all files in a folder, regardless of their extension. In this case, use:

String list[] = new File("C:\\Users\\Eric\\Desktop\\txt\\").list(new FileFilter() {
    public boolean accept(File file) {
        return file.isFile();
    }
});

Edit: Ok, I did not understand you correctly. You want to strip the extension from the file name in your if statement. You can use substring combined with lastIndexOf :

String name = list[i];
String nameWithoutExtension = name.substring(0, name.lastIndexOf("."));

or, since all your extensions are ".txt", the following will work too:

String nameWithoutExtension = name.substring(0, name.lastIndexOf(".txt"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM