//Author: Darin Park
//Date: 24 October, 2014
//Version: 2
package readandcopyupdate1;
import java.util.ArrayList;
import java.io.*;
public class ReadAndCopyUpdate1{
public static void main(String[] args){
ReadAndCopyUpdate1 rc = new ReadAndCopyUpdate1();
final File folder1 = new File("/root/avatar/default/upload/member");
final File folder2 = new File("/root/avatar/default/upload/Transfer");
rc.listFilesForOldFolder(folder1,rc.oldFiles);
rc.listFilesForNewFolder(folder2,rc.newFiles);
rc.oldFiles.stream().forEach((oldFile) -> {
System.out.println(oldFile);
});
System.out.println("\n\n");
rc.newFiles.stream().forEach((newFile) -> {
System.out.println(newFile);
});
}
private void listFilesForOldFolder(final File folder, ArrayList arrayList) {
/*
* This method takes two arguments.
* The first argument is the Original Avatar Upload Folder which we want to scan.
* The second argument is the ArrayList where we want to store all the avatar file names.
* The first argument is not a string, it's a File.
* So we need to first convert string to a File by using File Method. Look below for example.
* final File folder1 = new File("/root/avatar/default/upload/member");
*/
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForOldFolder(fileEntry, arrayList);
} else {
String str = fileEntry.getName();
if(str.equals("index.html")){
continue;
}
if(str.charAt(32) == '9'){
arrayList.add(str);
}
}
}
}
private void listFilesForNewFolder(final File folder, ArrayList arrayList) {
/*
* This method takes two arguments.
* The first argument is the New Transfer folder where we want to store avatar's copy.
* The second argument is the ArrayList where we want to store all the avatar names in the new Transfer folder.
* The first argument is not a string, it's a File.
* So we need to first convert string to a file by using File method. Look below for example.
* final File folder2 = new File("/root/avatar/default/upload/Transfer");
*/
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {
listFilesForNewFolder(fileEntry, arrayList);
} else {
String str = fileEntry.getName();
arrayList.add(str);
}
}
}
private final ArrayList<String> oldFiles = new ArrayList<>(5);
private final ArrayList<String> newFiles = new ArrayList<>(5);
}
These method used to work in my first version without any problem and now they are suddenly throwing null pointer exception when I am rewriting the code and was doing unit testing for these methods.
Exception in thread "main" java.lang.NullPointerException
at readandcopyupdate1.ReadAndCopyUpdate1.listFilesForOldFolder(ReadAndCopyUpdate1.java:33)
at readandcopyupdate1.ReadAndCopyUpdate1.main(ReadAndCopyUpdate1.java:15)
What I am trying to accomplish is the following: 1. folder1 consist of files which have 33 characters in their file name. I want to pick all those files which have number 9 in their 33th position.
- I'll store all such file names in an arrayList 'oldFiles'
- I'll scan folder2 and store all files there in arrayList 'newFiles'
- I am testing them by printing arrayLists values on sceen.