1

I wish to traverse through multiple files till it finds .zip files or .xml files

I have files which contain multiple folders and at the end of each folders it has the .zip files and .xml files , while a few folders might not even have that

following is my code :

package Traversefile; import java.io.File; public class traversefile { /** * @param args */ static String[] str1; static File dir = new File("/home/evangelist/newdata"); private static void traverse(File dir) throws NullPointerException{ if (dir.isDirectory()) { String[] children = dir.list(); for (int i=0; i<children.length; i++) { traverse(new File(dir, children[i])); } } if (dir.isFile()) { str1 = dir.list(); } for (int i=0; i<str1.length; i++) { System.out.println("filename:"+ str1[i]); } } public static void main(String[] args) { traverse(dir); // TODO Auto-generated method stub } } 

changes that i have made to check the traversing : package Traversefile;

import java.io.File;

public class traversefile {

/** * @param args */ static String[] str1; //static String homePath = System.getProperty("user.home"); static File dir = new File("/home/evangelist/newdata/nnc2/pairtree_root/ar/k+/=1/39/60/=t"); static int counter = 0; static int kounter = 0; static int krounter = 0; private static void traverse(File dir) throws NullPointerException{ if(dir.isDirectory()) { counter ++; String[] children = dir.list(); //System.out.println(children); for (int i=0; children != null && i<children.length; i++) { //System.out.println(children[i]); //System.out.println(" yo + "+ counter ); traverse(new File(dir,children[i])); } } if (dir.isFile()) { krounter++; if (dir.getName().endsWith(".xml")) //dir.getName().endsWith(".zip") || { System.out.println(dir.getAbsolutePath()); //System.out.println(krounter); } } } public static void main(String[] args) { traverse(dir); // TODO Auto-generated method stub } 

}

It gives me a null point exception at line 27 and 19 ..

how can I traverse through n number of folders and finally when I find the .zip znd .xml file copy their locations

Any help will be appreaciated

3
  • 2
    What are lines 27 and 19? Commented Mar 3, 2013 at 1:31
  • traverse(new File(dir, children[i]) -- 19 and for (int i=0; i<str1.length; i++)-- 27 .. thank you Commented Mar 3, 2013 at 1:33
  • dir.list() might return null check the javadoc docs.oracle.com/javase/6/docs/api/java/io/File.html#list() .... if (dir.isFile()) and str1 = dir.list(); is a bad combination, you cannot list a file you can only list a directory ... and str1 is always assigned a null Commented Mar 3, 2013 at 1:34

3 Answers 3

4

Perhaps this code can help you.

private static void traverse(File dir){ if (dir.isDirectory()) { String[] children = dir.list(); for (int i = 0; children != null && i < children.length; i++) { traverse(new File(dir, children[i])); } } if (dir.isFile()) { if (dir.getName().endsWith(".zip") || dir.getName().endsWith(".xml")) { System.out.println(dir.getAbsolutePath());//change it if needed } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

This has helped a lot , but for some reason it skips the first few folders in finding the .xml files ... My traverse function skips a few folders , any reason that you might know .
It could be because those files do not end with ".xml" but with ".xml ", use trim() ex. getName().trim().endsWith() or because you don't have permission to list those files. Try to run it as administrator. Remove the if(...endwith() to print all the files found and check whether they are there or not. I hope this helps.
1

It looks like you're trying to reference a file in your computers file system. That being the case, you cant just say "/home/evangelist/newdata". First, you need to get a reference to the home directory by calling String homePath = System.getProperty("user.home"); Then you can say static File dir = new File(homePath + "evangelist/newdata");, assuming evangelist is a folder in your home directory.

By default, Java assumes that any files referenced are in the program's working directory.

2 Comments

the above option does not really work ... is.Directory fails here
@drewmore4 /home/evangelist/newdata looks like an absolute path (starting with /), hence, this should be fine to refer in java. If you are refering a relative path (not starting with /), then you have to do the way you suggested above. Quoting from java API "...For UNIX platforms, the prefix of an absolute pathname is always "/". Relative pathnames have no prefix."
1

Following snippet is wrong. a simple File has no children, hence, list() will return null.

if (dir.isFile()) { str1 = dir.list(); } 

here str1 will be null if dir is a file.

EDIT: link to java File.list() API. It clearly mentions list() will return null in case the object is not a directory http://docs.oracle.com/javase/6/docs/api/java/io/File.html#list().

4 Comments

No dir is a file at this point since you say if (dir.isFile())
@drewmore4 in your if() you are checking if dir object isFile(). If that condition is true, you get inside the block and try to list() sub-folders of a file, which will give you null, hence NPE.
@BimaleshJha you are correct it was giving me an error because fir could become a file if it traverses.. so i changed it .. but how will i find my solution
@njack take a look at "FilenameFilter" and make use of it to list only files ending with .zip or .xml. You can google for FilenameFilter in java. The link to java APIs for filter: docs.oracle.com/javase/6/docs/api/java/io/FilenameFilter.html

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.