0

I have a folder "files" in my project. I want to create a query of all files in it. 1. How to open that folder without specifying the full path? 2. How to put all files from my folder to this query?

ArrayDeque<File> files = new ArrayDeque<File>();

I wrote some code. But program give me the name of folder instead the name of files in it. So my program don't find a folder in my project-folder. How I can get my folder "folder1" without all path? It's my code

public static void main(String args[])
{
    File file = new File("<folder1>");
    ArrayDeque<File> queue = new ArrayDeque<File>();
    filesQueue(file, queue);
    System.out.print(queue.getFirst());
}

public static ArrayDeque<File> filesQueue(File f,  ArrayDeque<File> queue) {
        if (f.isDirectory()) {
            for (File file : f.listFiles()) {
                filesQueue(file, queue);
            }
        } else {
            queue.addLast(f);
        }

    return queue;
}
Mark Beras
  • 39
  • 6
  • 1
    Possible duplicate of [Read all files in a folder](http://stackoverflow.com/questions/1844688/read-all-files-in-a-folder) – user2004685 Feb 26 '16 at 12:41

2 Answers2

1
File file=new File("<folder-name>");
ArrayDeque<File> filesQueue=listFiles(file);

static ArrayDeque<File> fileList=new ArrayDeque<File>();
public ArrayDeque<File> listFiles(File folder) {

    for (File f : folder.listFiles()) {
        if (f.isDirectory()) {
            listFiles(f);
        } else {
           fileList.add(f);
        }
    }
return fileList;
}
Musaddique S
  • 1,539
  • 2
  • 15
  • 36
0
File folder = new File("src/folder1");
Mark Beras
  • 39
  • 6
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. - [From review](https://stackoverflow.com/review/low-quality-posts/11423454) – Ferrybig Feb 27 '16 at 21:35