How to get all text files from one folder using Java?

后端 未结 5 648
渐次进展
渐次进展 2020-12-11 18:36

I need to read all \".txt\" files from folder (user needs to select this folder).

Please advise how to do it?

5条回答
  •  臣服心动
    2020-12-11 18:56

    Assuming you already have the directory, you can do something like this:

    File directory= new File("user submits directory");
    for (File file : directory.listFiles())
    {
       if (FileNameUtils.getExtension(file.getName()).equals("txt"))
       {
           //dom something here.
       }
    }
    

    The FileNameUtils.getExtension() can be found here.

    Edit: What you seem to want to do is to access the file structure from the web browser. According to this previous SO post, what you want to do is not possible due to security reasons.

提交回复
热议问题