Results 1 to 6 of 6
- 01-20-2009, 02:46 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 3
- Rep Power
- 0
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
-
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)
- 01-20-2009, 03:24 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 3
- Rep Power
- 0
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")...
- 01-20-2009, 03:57 PM #4
Member
- Join Date
- Jan 2009
- Posts
- 3
- Rep Power
- 0
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); }
- 01-21-2009, 12:36 AM #5
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.
- 01-27-2010, 09:14 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
WHy does it say imcompatable types?
By PeterFeng in forum New To JavaReplies: 3Last Post: 01-14-2009, 05:54 PM -
Creating Folder in JAVA and Copying File
By fnoman in forum New To JavaReplies: 8Last Post: 10-18-2008, 07:33 AM -
how to access shared file/folder in LAN
By ksheetiz in forum NetworkingReplies: 1Last Post: 05-24-2008, 04:54 AM -
Use Scanner to read various types of data from a file
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks