Jump to content

How to make my program only detect a certain file extension ?

- - - - -

  • Please log in to reply
3 replies to this topic

#1
xxxxjayxxx

xxxxjayxxx

    Programmer

  • Members
  • PipPipPipPip
  • 123 posts
I need to open 200 .xml file at 1 go, Like let's say that in the folder contain 2 to 3 .txt

How can I make my program prompt an error message indicate that there are other files other than .xml file found in the folder ?

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Open the folder using the java.io.File class, then use .list() to get a String array of all the filenames in there.
File file = new File("C:\\");
for(String name: file.list()){
    System.out.println(name);
}
This shows all the files (and folders) in c:\. You'll have to loop trough the whole list and make sure every file ends with ".xml" (String has a .endsWith() method)

#3
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I was glancing through the api and came across the listFiles method provided by the File class.
Link: File (Java Platform SE 6)

If you create a class that implements the FilenameFilter interface, you can define what files are returned by the listFiles method.
More specifically, you can return a list of files[] that have the extension you want.

Here is also a quick way to implement this idea:
Listing the Files or Subdirectories in a Directory | Example Depot

#4
mokszyk

mokszyk

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
This code is very easy, don't worry about my logic code.
You will be able understand it. Did you seek something like this? :

private void listujPliki(String path) throws FileNotFoundException {
        ArrayList<String> fcatalogs = new ArrayList<String>();
        ArrayList<String> ffiles = new ArrayList<String>();
        File dir = new File(path);

        /*filtration xml or catalog*/
        File[] files = dir.listFiles(new FileFilter() {
            public boolean accept(File pathname) {
                String extension = "";
                if (pathname.isFile()) {
                    String ext = pathname.toString();
                    int length = ext.length();
                    extension = ext.substring(length - 3);
                }

                if (extension.equals("xml") || pathname.isDirectory())
                    return true;
                else
                    return false;  //To change body of implemented methods use File | Settings | File Templates.
            }
        });

        /*list files*/
        for (File f : files) {
            if (!f.isDirectory()) {
                ffiles.add(f.toString());
                //System.out.println("\tFound file: " + f.toString());
            }
        }

        /*list folders*/
        for (File f : files) {
            if (f.isDirectory()) {
                fcatalogs.add(f.toString());
                //System.out.println("\t\tFound directory also.. ->[d]" + f.toString());
                }
            }
        }


        /*operation on xml's*/
         for (String s : ffiles) {
             // ...
        }

        /*run this method in every folders*/
        for (String s : fcatalogs) {
            listujPliki(s);
        }

    }

First you declare something to store files and folders. Then we list all items in directiory. If it is file with extension xml we add to list of files. Otherwise if it is not with xml extension we add it to catalog. Then we use recurenction to use this the same method in all folders.
THink positive :)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users