从没有扩展名的文件夹输出文件 [英] output files from the folder with no extension

查看:110
本文介绍了从没有扩展名的文件夹输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

帮助实现此目的,以下代码查找文件夹中的txt文件,以及如何使其列出没有扩展名的文件名。以下是代码:

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");

如你所见,就在这里:

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

这里是文件列表与myname的值进行比较,所以你能帮忙给出比较表和myName和list [i],文件扩展名没有被考虑)
如程序显示那个文件夹有以下布局:indeks.html hi.html,用户输入indeks.html,比较好,但是如何创建一个以便用户输入代码,程序仍然将它与文件列表进行比较目录?

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();
    }
});

编辑:好的,我没理解你。您想从if语句中的文件名中删除扩展名。您可以使用 substring 结合 lastIndexOf

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("."));

或者,由于您的所有扩展名都是.txt,以下内容也适用:

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

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

这篇关于从没有扩展名的文件夹输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆