My requirement is to get list of files of required file extension from different directories and while getting the files itself i should get the input stream and do some processing.... I am trying to get the list of files with required file extension, then I am trying to get input stream from all the selected files in that list. For the first iteration, i am getting the required files and also i am getting the stream for one file but for the next file, i get a null pointer exception. I am also unable to get the list of files from the second iteration
Sample code that i used to test:
System.out.println(ftp.printWorkingDirectory());
boolean status = ftp
.changeWorkingDirectory("mydirectory");
System.out.println("Status of Change Directory:" + status);
System.out.println(ftp.printWorkingDirectory());
InputStream is = null;
System.out.println(ftp.printWorkingDirectory());
System.out.println(ftp.isConnected());
FTPFile[] list2 = ftp.listFiles();
System.out.println("Number of files in this directory:"
+ list2.length);
for (int i = 0; i < list2.length; i++) {
System.out.println("-------[" + list2[i].getName()
+ "]---------------------");
is = ftp.retrieveFileStream(list2[i].getName());
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = is.read(buffer, 0, buffer.length)) > 0) {
System.out.println(new String(buffer, 0, bytesRead));
}
//is.close();
System.out.println("-------[END:" + list2[i].getName()
+ "]---------------------");
}
The count of files is shown as 3 and the first file in the 'mydirectory' is read correctly but when it tries to read the second file it says null pointer exception .... also, after i read the stream using retrieveFileStream method, if i try to print current working directory, it gives null value but says it is still connected....
Please let me know if i have some bugs in my code.... The only workaround for me was to connect to the ftp location for every input stream read which is not a good thing to do....