Results 1 to 6 of 6
  1. #1
    Mippzon is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default How to get all filenames of certain file types in a folder?

    Hey!

    I have built a small program that opens a csv-file and extracts some data from it. The user specify what file to be used by writing the file name in the prompt. How could I change my small program to automatically go over all files in a certain folder, instead of just one file at a time?

    Thank you in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Is this a non-GUI program? If so, how are you getting the file list, via File#listFiles() method? If so, there's an overload of this method that takes a FileFilter object that you should look into.
    Look here: File (Java Platform SE 6)
    and here: FileFilter (Java Platform SE 6)

  3. #3
    Mippzon is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default

    Ah, thanks. I'll take a look there. Yes, its a non-GUI program at the moment. Im using something like new Filereader("file name")...

  4. #4
    Mippzon is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Default Found some code, but got another problem

    I found a code that could do what I want, and it does it very good. But I am puzzled over one thing. I have some log files in a folder. All logs that are named 20081001 to 20081031 works great. But if I use files that are not named like this, I get an error saying the file is not present and cannot be open. Any suggestions?

    Java Code:
            
    
    List<String> filnamn = new ArrayList();
    
            File folder = new File("c:/Log");
            File[] listOfFiles = folder.listFiles();
    
            for (int i = 0; i < listOfFiles.length; i++) {
                if (listOfFiles[i].isFile()) {
                    filnamn.add(listOfFiles[i].getName());
                    System.out.println(listOfFiles[i].getName());
    
                }
            }
    
            for (int i = 0; i <= filnamn.size() - 1; i++) {
    
                String teststr = filnamn.get(0);
                
                CSVReader csvread = new CSVReader(new FileReader(teststr));
                List<String[]> test = csvread.readAll();
                Filter testfilter = new Filter(test);
            }

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    You should check that each file is a file and not a directory. If that's not the issue, look at the file that is having the problem in debug and see if it has any unusual properties.

  6. #6
    tonyik is offline Member
    Join Date
    Jan 2010
    Posts
    1
    Rep Power
    0

    Default

    append the path to the file name as:

    String dir = "c://log";
    CSVReader csvread = new CSVReader(new FileReader(dir+"/"+teststr));

    note the "/" b4 the filename

Similar Threads

  1. WHy does it say imcompatable types?
    By PeterFeng in forum New To Java
    Replies: 3
    Last Post: 01-14-2009, 06:54 PM
  2. Creating Folder in JAVA and Copying File
    By fnoman in forum New To Java
    Replies: 8
    Last Post: 10-18-2008, 08:33 AM
  3. how to access shared file/folder in LAN
    By ksheetiz in forum Networking
    Replies: 1
    Last Post: 05-24-2008, 05:54 AM
  4. Replies: 0
    Last Post: 04-17-2008, 08:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •