6

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

Please advise how to do it?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
DmitryB
  • 1,149
  • 5
  • 20
  • 34

5 Answers5

12

you can use filenamefilter class it is pretty simple usage

public static void main(String[] args) throws IOException {

        File f = new File("c:\\mydirectory");

        FilenameFilter textFilter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(".txt");
            }
        };

        File[] files = f.listFiles(textFilter);
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.print("directory:");
            } else {
                System.out.print("     file:");
            }
            System.out.println(file.getCanonicalPath());
        }

    }

just create an filenamefilter instance an override accept method how you want

RAGINROSE
  • 694
  • 8
  • 13
daemonThread
  • 243
  • 2
  • 12
2

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.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
  • this works ,when user selects a file ,pass the path to File constructor – Balaswamy Vaddeman Jan 19 '12 at 06:48
  • This will only work if the selected folder is at server side (which does not seem to be the case here). You can not access a folder at client side from server side. – Manish Jan 19 '12 at 06:54
  • @ManishSharma: Regarding your first point, the OP did not specify any scenarios... seeing that spring controllers are located on the server, the assumption was that the OP wanted to access stuff on the server side. With regards to your second point, the OP did not specify anything which he has done. I simply provided a small segment of code which did what the OP has asked for, the purpose of this site is not to make the work of others. – npinti Jan 19 '12 at 06:59
  • @ManishSharma: Is it possible to do this on client side without copying files on the server? – DmitryB Jan 19 '12 at 07:05
1

You need to read the directory and iterate inside it.

it is more a question on Java access to file systems than about MVC

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
1

I wrote the following function that will search for all the text files inside a directory.

public static void parseDir(File dirPath)
    {

        File files[] = null;
        if(dirPath.isDirectory())
        {
            files = dirPath.listFiles();
            for(File dirFiles:files)
            {

                if(dirFiles.isDirectory())
                {
                    parseDir(dirFiles);
                }
                else
                {
                    if(dirFiles.getName().endsWith(".txt"))
                    {
                        //do your processing here....
                    }
                }
            }

        }
        else
        {
            if(dirPath.getName().endsWith(".txt"))
            {
                //do your processing here....
            }
        }

    }

see if this helps.

Shashank Kadne
  • 7,993
  • 6
  • 41
  • 54
1

provide a text box to user to enter the path of directory.

File userDir=new File("userEnteredDir");
File[] allfiles=useDir.listFiles();

Iterate allFiles to filter .txt files using getExtension() method

Balaswamy Vaddeman
  • 8,360
  • 3
  • 30
  • 40