3

I'd like to check for an existing file using a while loop. Now the problem is, that if use something like this:

while (file.exists()) {
        text.setText("Some text appears");
        text.setTextColor(Color.RED);
}

my program always seem to not to respond at all. Is it because my loop is somehow an infinite loop? Or why is is not working correctly.

Right now, i am using a simple if statement but i don't like it that way, because it is not updated right away when the file exists.

EDIT: What i want is: I offer a file to download. In my app, there is a text which says "Not Available yet". I want to change the text right after the file exists to something like "File is Available".

user3801167
  • 177
  • 1
  • 2
  • 7
  • 3
    Yes, it is an infinite loop. It keeps checking a condition that is always true, so it never exists the loop. What is it that you want to do? – luanjot Aug 22 '14 at 18:28
  • You need to change the file object i the loop. in your case you ever ask for the same file!! – Jens Aug 22 '14 at 18:28
  • 1
    It would help if you described exactly what it is you are trying to do and posted the code are you working with. – zero_dev Aug 22 '14 at 18:28
  • you're checking if the file exists, why not check if the file doesn't exist – j.con Aug 22 '14 at 18:34

4 Answers4

3

If you want to check periodically if a file exists, you have to do this with an asynchronous task or a timer.

    TTimerTask task = new TimerTask() {

        @Override
        public void run() {
            // Here you do whatever you want
        }
    };

    Timer timer = new Timer();
    timer.schedule(task, 0,30000);

This will check the file every thirty seconds.

You can find more info on http://www.mkyong.com/java/jdk-timer-scheduler-example/

luanjot
  • 1,176
  • 1
  • 7
  • 21
  • 1
    It's hard to tell at this point if that was the OP intent. For that matter though, I'd would also consider a [WatchService](http://docs.oracle.com/javase/tutorial/essential/io/notification.html). – Edwin Dalorzo Aug 22 '14 at 18:42
  • The OP says he wants to check when the file is available and change the text... so, I guess this is what he needs. Of course, there are other possible solutions. – luanjot Aug 22 '14 at 18:43
  • 1
    But that is what he says he wants to change "not available *yet*" for "available". I think the time is of essence. – luanjot Aug 22 '14 at 18:45
  • 1
    I had not read the edited text of the question. You're right. – Edwin Dalorzo Aug 22 '14 at 18:47
  • 1
    @EdwinDalorzo Your pointer to [WatchService][http://docs.oracle.com/javase/tutorial/essential/io/notification.html] is more valuable than all the answers combined. Events/notifications are the way to go, not polling. – user3603546 Aug 23 '14 at 14:58
2

Your program goes in an infinite loop as the condition inside while loop will always be true if the file is present..

You need to check like this:

File file = new File(subDir.getPath() + "somefile.txt");  
boolean exists = file.exists();  

if (!exists) {  
// It returns false if File or directory does not exist 
}
else
{
  //Update here
}

And if you want to check it inside the loop then try like this:

while (true)
{
  File file = new File(subDir.getPath() + "somefile.txt");  
  boolean exists = file.exists();  

  if (!exists) {  
    // It returns false if File or directory does not exist 
    return;
  }
  else
  {
    //Update here
  }
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
1

If the file exists, then it'll pop into the while loop and will keep on looping because the file exists, you'll have to make the file non-existable within the while loop...

Best thing for you to do, is get your program working without the while loop... as you mentioned with an IF function, then slowly over time implement (Test) a new function (while loop) into the equation.

What you need to fix in the while loop is... What happens to the file when it enters the loop and how does it get out of the loop. Current standing is, it doesn't as the file still exists.

Paulie
  • 6,535
  • 4
  • 20
  • 35
0

Ok so whenever the other person is checking for the file does the program exit if the file doesn't exist? I mean you can't just have it checking for the file forever. I suppose you could put it in a Thread and run it with a slight wait inside the while loop but the overhead... sheesh!

Is this a specific screen inside your program? Is the person suppose to be able to exit this "file found/not found" page? I could probably write a snippet but I need more info. :)

Philip Vaughn
  • 606
  • 6
  • 20