Results 1 to 2 of 2
Thread: isFile()method
- 05-24-2008, 02:09 PM #1
Member
- Join Date
- May 2008
- Posts
- 6
- Rep Power
- 0
isFile()method
Hi everyone, i have a problem with isFile()method.
It seems that it doesn't recognize betwwen directories and normal files.
I have a directory with music and is like that. MyMusic/Dir1/Dir2/Dir3 but also there are mp3 files and jpeg files inside Mymusic not in any directory.
I use listFiles with filter .isDirectory to return only the paths of the directories but except from the dirs it also returns the mp3 files and the pictures.
I test it also with some other ways like return dir.isDirectory || !idir.isFile()
but the same again. Lastly when i called inly the dir.isFile() it returns nothing(null) like there are no files indise the dir.
Did anyone had the same problem before or knows how to overcome
Thanks you
The part of the code :
String output[] ={};
String paths ="";
paths = getTreePath((tr.getTree()).getSelectionPath(),the_ Seperator);
//File name = new File(paths);
// output = name.list();
JOptionPane.showMessageDialog(null,paths,"paths",J OptionPane.INFORMATION_MESSAGE);
File dir = new File(paths);
File[] filesWanted = dir.listFiles(
new FilenameFilter() {
public boolean accept(File dir, String name) {
return dir.isDirectory();
- 05-25-2008, 08:34 PM #2
There is some subtlety here.
Java Code:import java.awt.Dimension; import java.io.*; import java.util.*; import java.util.List; import javax.swing.*; public class FilteringFiles { public static void main(String[] args) { File currentDirectory = new File("."); System.out.println("number of files = " + currentDirectory.listFiles().length); // Show image files in currentDirectory. File[] files = currentDirectory.listFiles(imageFilter); System.out.println("image files = " + files.length); showFiles(files); // Show image files in folders contained in currentDirectory. List<File> folderList = new ArrayList<File>(); getAllFolders(currentDirectory, folderList); List<File> imageList = extractImageFiles(folderList); File[] allFiles = imageList.toArray(new File[imageList.size()]); System.out.printf("folders = %d images in folders = %d%n", folderList.size(), allFiles.length); showFiles(allFiles); } private static void showFiles(File[] files) { JTextArea textArea = new JTextArea(5,20); for(int i = 0; i < files.length; i++) { String name = files[i].getName(); textArea.append(name + "\n"); } JScrollPane scrollPane = new JScrollPane(textArea); Dimension d = scrollPane.getPreferredSize(); d.height = 300; scrollPane.setPreferredSize(d); JOptionPane.showMessageDialog(null, scrollPane, "", -1); } private static void getAllFolders(File parent, List<File> list) { File[] files = parent.listFiles(); for(int i = 0; i < files.length; i++) { if(files[i].isDirectory()) { list.add(files[i]); getAllFolders(files[i], list); } } } private static List<File> extractImageFiles(List<File> folders) { List<File> imageFiles = new ArrayList<File>(); for(int i = 0; i < folders.size(); i++) { File[] files = folders.get(i).listFiles(imageFilter); imageFiles.addAll(Arrays.asList(files)); } return imageFiles; } private static FileFilter imageFilter = new FileFilter() { final static String BMP = "bmp"; final static String GIF = "gif"; final static String JPG = "jpg"; final static String JPEG = "jpeg"; final static String PNG = "png"; public boolean accept(File file) { String ext = getExtension(file); return ext.equals(BMP) || ext.equals(GIF) || ext.equals(JPG) || ext.equals(JPEG) || ext.equals(PNG); } private String getExtension(File file) { String s = file.getPath(); int dot = s.lastIndexOf("."); if(dot > -1 && dot < s.length()-1) return s.substring(dot+1); return null; } }; }
Similar Threads
-
method not abstract, does not override actionperformed method.
By Theman in forum New To JavaReplies: 2Last Post: 03-26-2010, 05:12 PM -
cannot call private method from static method
By jon80 in forum New To JavaReplies: 3Last Post: 05-07-2008, 08:37 AM -
Method Help
By pringle in forum New To JavaReplies: 4Last Post: 04-16-2008, 01:23 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks