how to read files in whole directory
hi everybosy i have got a directory called grepdatabase in the following path
/users/zer/gd.
it has 3000 files in it.i have to read each file and do soem process and write it into another file.
but with the following code it says, directorydoesnot exists.but i have the directory,with all the files in it.
File database =new File("c:/Domain/nbi.bbsrc.ac.uk/Users/zer/gd");
if (database.exists() && database.isDirectory()){
String datafiles[] = database.list();
for (int i = 0; i < datafiles.length; i++){
System.out.println("processing file: " + database.getAbsolutePath() +
"/" + datafiles[0]);
}
}else{
System.out.println("Database exists: "+database.exists()+"\nDatabase is Directory: "+database.isDirectory());
}
can anyone help me how to read multiple files...
Hi,
Just couple of small mistakes....
1. use escape sequence,
2. iterate through i in the loop and also use the index 'i' to print those file path.
File database = new File("c:\\Domain\\nbi.bbsrc.ac.uk\\Users\\zer\\gd");
if (database.exists() && database.isDirectory()) {
String datafiles[] = database.list();
for (int i = 0; i < datafiles.length; i++) {
System.out.println("processing file: " + database.getAbsolutePath() +
"\\" + datafiles);
}
} else {
System.out.println("Database exists: "+database.exists()+"\nDatabase is Directory: "+database.isDirectory());
}
Cheers.
Message was edited by:
jini4java
hi ,i am unix user so i cant use \\,i suppose and the problem is i have a directory with lots of fiels already,but the code is not recognisisng those directory and files ,though i have given the same path.i cant find what the problem is.
ur code can be little modified as follows to make it work flawlessly
File database = new File("c:/Domain/nbi.bbsrc.ac.uk/Users/zer/gd");
if (database.exists() && database.isDirectory()) {
//String datafiles[] = database.list();
//Instead use file list here
File[] datafiles = database.listFiles();
for (int i = 0; i < datafiles.length; i++) {
//System.out.println("processing file: " + database.getAbsolutePath() +
//"/" + datafiles[0]);
System.out.println("processing file: " + datafiles[i].getName());
}
} else {
System.out.println("Database exists: "+database.exists()+"\nDatabase is Directory: "+database.isDirectory());
}
BR