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
if (dir.isFile())andstr1 = dir.list();is a bad combination, you cannot list a file you can only list a directory ... andstr1is always assigned anull