0

I load some files from a folder which have names file 1.pdf, file 2.pdf etc I want to load them all to my program. If I new the total number of them, I would do sth like:

for(int i=1; i<=100; i++){
   File pdfFile = new File("file "+i+".pdf");
   //...
}

But I don't know the total number.. What would be the condition so when there is no other file, then exit ?

do{
      File pdfFile = new File("file "+i+".pdf");
      //...
}while(//there are files...)
yaylitzis
  • 5,354
  • 17
  • 62
  • 107

4 Answers4

7

There are two approaches to this - you could simply keep incrementing your counter until you get a file that doesn't exist, i.e.

int i = 1;
File file;
while((file = new File("file "+i+".pdf")).exists()) {
    // do whatever
    ++i;
}

The imho less clumsy approach would be to simply iterate over all files in a particular directory and just ignore those with names not matching your pattern (that is, unless the order in which you're processing your files is important).

Cubic
  • 14,902
  • 5
  • 47
  • 92
  • 1
    How does this work? Because `.exists()` returns a boolean? Edit: Ah I see how it should work, but you are missing a `)`. – martijnn2008 Feb 29 '16 at 11:27
  • @martijnn2008 `exists` returns true if the file exists, yes. This relies on the fact that assignment in Java, like in C and C++, is actually an expression with a value rather than a statement (specifically, it has the value that was assigned). – Cubic Feb 29 '16 at 11:29
1

You could catch an exception and break a while loop. Originally I had answered:

boolean success = true;
int i = 0;

while(success){

    i = i + 1;

    try{
        File pdfFile = new File("file " + i + ".pdf");
    } catch(FileNotFoundException ex){
        success = false;
    }
}

But I have been told that constructors do not throw exceptions, so this would not work. Therefore you would be best using the answer below, as another user (@cubic) suggested.

int i = 0;
File file;

while((file = new File("file " + i + ".pdf")).exists()){
    // something with file
    i++;
}
JCollerton
  • 3,227
  • 2
  • 20
  • 25
  • 4
    it doesn't seem like this is going to help. there is no exception thrown by the constructor when the file doesn't exist (see http://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-in-java) – Breeze Feb 29 '16 at 11:25
  • Ahh, I didn't know that! I will edit this. – JCollerton Feb 29 '16 at 11:27
  • 1
    The second part of you answer was already given by someone else, and it was wrong initially. – martijnn2008 Feb 29 '16 at 11:33
  • 1
    Edited to explain why my method wouldn't work (and accepted your edit as well as credit the proper answer). – JCollerton Feb 29 '16 at 11:37
1

If you have access to the dependency Apache Commons IO, your code would look like this:

File dir = new File("C:\\Users\\your.directory\\");
FileFilter fileFilter = new WildcardFileFilter("file *.pdf");
File[] files = dir.listFiles(fileFilter);
for(File file : files){
    System.out.println(file.getAbsolutePath());
}
0

You should count number files into the folder

new File(<directory path>).listFiles().length
biology.info
  • 3,500
  • 2
  • 28
  • 39