I was trying to write a methods to get all directories and files in a directory and finally decided to give up and ask here.
I know this has been asked a few times and has been answered but this is a little harder.
I did get this code
public static void listf(String directoryName, ArrayList<File> files)
{
File directory = new File(directoryName);
// get all the files from a directory
File[] fList = directory.listFiles();
for (File file : fList)
{
if (file.isFile())
{
files.add(file);
} else if (file.isDirectory())
{
listf(file.getAbsolutePath(), files);
}
}
System.out.println(files);
}
and it helped a lot but i need it to also give the directory it was in ei.
C:\\Users\\UserName\\Desktop\\Folder\\Folder1\\a.txt
C:\\Users\\UserName\\Desktop\\Folder\\Folder1\\b.txt
C:\\Users\\UserName\\Desktop\\Folder\\Folder2\\c.txt
My first code:
public class FileTransfer
{
private final static File testFileFolder = new File("C:\\Users\\Melaia\\Desktop\\Send\\");
private static File[] filesInFolder;
private static String[] listOfFilesInFolder;
private static int noOfFilesInFolder, k = 0;
public static void startupFileSend()
{
filesInFolder = testFileFolder.listFiles();
noOfFilesInFolder = (filesInFolder.length);
for(int zzz = 0; zzz <= noOfFilesInFolder; zzz++)
{
if(filesInFolder[k].isDirectory())
{
File[] file1 = filesInFolder[k].listFiles();
listOfFilesInFolder[k] = file1[k].getName() + ";";
}
else
{
listOfFilesInFolder[k] = filesInFolder[k].getName();
}
System.out.println(listOfFilesInFolder[k]);
}
}
}
but that gives me this exception:
Exception in thread "main" java.lang.NullPointerException
at Com.org.FileTransfer.startupFileSend(FileTransfer.java:32)
at Com.org.Main.main(Main.java:7)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Please can someone give me any advice on this.
Thanks Luke.