How to check if file exist ?

Put your problem here if it does not fit any of the other categories.

How to check if file exist ?

Postby sasuke » Sun Jan 27, 2008 6:41 am

Hello all,

What's the easiest way to check if a file exist or not ? I use the :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. File f = new File("f.txt");
  3.  
  4. if (f.exist()) {
  5.  
  6. ...
  7.  
  8. }
  9.  
  10.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


it's not working...

Regards,
sasuke
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Top

Re: How to check if file exist ?

Postby tum0rc0re » Sun Jan 27, 2008 6:53 am

sasuke wrote:Hello all,

What's the easiest way to check if a file exist or not ? I use the :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. File f = new File("f.txt");
  2. if (f.exist()) {
  3. ...
  4. }
  5.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


it's not working...

Regards,
sasuke


It's right, simply you have to specify the file name as "fullpathfilename"

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. boolean exists = (new File("fullpathfilename")).exists();
  2. if (exists) {
  3.   // File or directory exists
  4. } else {
  5.   // File or directory does not exist
  6. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
JQ Soft web page:
[b][url=http://jqsoft.ru/]http://jqsoft.ru/[/url][/b]
My proffesional blog: [b][url=http://maximyudin.blogspot.com]http://maximyudin.blogspot.com[/url][/b]
Facebook: [b][url=http://www.facebook.com/maxim.yudin]http://www.facebook.com/maxim.yudin[/url][/b]
User avatar
tum0rc0re
Senior Developer
Senior Developer
 
Posts: 158
Joined: Sun Nov 25, 2007 7:15 am
Location: Moscow, Russia

Postby sasuke » Sun Jan 27, 2008 7:16 am

@tum0rc0re hmm.. doesn't work either...

android won't create the file if already exist, but how to check it?

Regards,
sasuke
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby plusminus » Sun Jan 27, 2008 12:26 pm

Hello sasuke,

what about doing an openFileInput(path); and if the file doesn't exist you can catch the FileNotFound-Exception :?:

(untested :!: + didn't have a look at the sdk-documentation :( )

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby sasuke » Sun Jan 27, 2008 1:21 pm

@plusminus, i'm sure that can be done..

i must check the file manually :lol:

Regards,
sasuke
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Postby cabernet1976 » Mon Jan 28, 2008 4:26 am

I have no experience about File.exist() for file system, but I am sure it can work fine for file in sdcard.
plusminus's way is smart.
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Top

Postby cabernet1976 » Mon Jan 28, 2008 4:30 am

I just think we'd better to create a class which can work for the file both in file system and sdcard.
How do you think about it?
Upload2Flickr's blog: http://upload2flickr.blogspot.com
cabernet1976
Senior Developer
Senior Developer
 
Posts: 154
Joined: Fri Nov 16, 2007 2:34 am
Location: China

Postby sasuke » Mon Jan 28, 2008 4:40 am

Hello cabernet1976,

It's a problem if we can't create a file with a specific path, and we can't retreive the file list within our app's path (data/data/<package>/) because the different between the security level..

cabernet1976 wrote:I just think we'd better to create a class which can work for the file both in file system and sdcard.
How do you think about it?


I've created a file that listed all the files that i created with my apps, so when i want to check the file list, delete a file, append, etc.. i just retrieved the file name from the index, and process it..

Regards,
sasuke
sasuke
Experienced Developer
Experienced Developer
 
Posts: 57
Joined: Fri Nov 30, 2007 1:25 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 19 guests