-1

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.

1
  • I 90% sure it, haven't updated to java 8 yet Commented Mar 21, 2014 at 23:18

2 Answers 2

2

1) initialize this one listOfFilesInFolder like this

listOfFilesInFolder = new String[noOfFilesInFolder]

2) Not sure if this is the only problem but change

zzz <= noOfFilesInFolder

to

zzz < noOfFilesInFolder

3) Also, you never change the k variable, not sure if that's intended.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the quick reply but that makes it run once more than i need it to, the problem has something to do with my String array but i cannot for the life of me find it.
Thanks for pointing that, i don't know how i missed that. thanks so much.
Also sorry but i also would like to ask 1 more small question its not very big so i don't think its worth posting a new question but when i first created my account i got a few messages saying a few restrictions have been removed for reputation increse but they dont work nor do i have any reputation. thanks in advance.
1

Since you are using Java 7, don't bother and use a FileVisitor.

Create a class, even anonymous, to manage all files in your directory, the Files.walkFileTree() method will walk the file for you and execute the code you have put in the visitor on the different events.

You may also extend SimpleFileVisitor instead. You can see two examples of visitors (one for recursive copy, another for recursive deletion) here.

Since you seem to want to upload, you'll probably want also to use Files.copy(), which can take a source path and send its content to an OutputStream.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.