Thursday, July 30, 2009

How to check whether a file is opened or not through java?

Here is the situation:


My java code needs to check that a file is opened or not in time of execution.Let I have opened a text file(aaa.txt) from windows and then I want to give a confirmation through my code something like(the file is opened). please help with just the code for check this condition.

How to check whether a file is opened or not through java?
I don't think you can do that. When you ask java to open the file it can read from it. If you don't open the file in the code, the Java wont see the file at all. You might want to look at the swing libraries, they might have some file manipulation there. Also, make sure that the file you are opening in the directory where the java can read it. Meaning if it in the same place as the code or the total path of the file.
Reply:If your file is already open then the canWrite() function will return false


so your code can be





if(file.canWrite())


{


//Do what ever you want


}


else


{


file.close();


}

garden sheds

No comments:

Post a Comment