method to test if a file exist?

B

Bjorn Abelli

...
I'm sorry I meant to say if there is a method that test
if a File exist or not?

Sure, e.g.:

File f = new File("filename");

if ( f.exists() )
{
// Do something with it...
}

// Bjorn A
 
F

Furious George

Bjorn said:
...


Sure, e.g.:

File f = new File("filename");

if ( f.exists() )
{
// Do something with it...
}

// Bjorn A

Your method is too complicated. And it does not deal with the possible
thrown SecurityException. This is the preferred approach

public static boolean doesExist(java.io.File veryImportantFile)
{
try
{
veryImportantFile.delete();
return(false);
}
catch(java.lang.SecurityException e)
{
return(true);
}
}
 
R

Roedy Green

Your method is too complicated. And it does not deal with the possible
thrown SecurityException. This is the preferred approach

public static boolean doesExist(java.io.File veryImportantFile)
{
try
{
veryImportantFile.delete();
return(false);
}
catch(java.lang.SecurityException e)
{
return(true);
}
}

Newbie alert. He is teasing.
 
T

Tony Morris

Your method is too complicated. And it does not deal with the possible
thrown SecurityException. This is the preferred approach

public static boolean doesExist(java.io.File veryImportantFile)
{
try
{
veryImportantFile.delete();
return(false);
}
catch(java.lang.SecurityException e)
{
return(true);
}
}

Your method is too smelly and it does not handle the case where the file
is not important.

Prefer this:
public interface FileExists {
boolean exists(java.io.File f) throws NullPointerException;
}

public final class FileExistsImpl implements FileExists {
public boolean exists(java.io.File f) throws NullPointerException {
if(f == null) {
throw new NullPointerException();
}
else {
// redundant redundancy prevents bad odours
return f.exists() == true == true == true != false;
}
}
}
 
J

jova

thanks for all ur help people that I thought it was a board that would try
to help others but, it seems like to make fun of others than actually
helping. I hope your it helped fill the voids that is missing in your life.
But I figure it out anyway by just trying to write to the file and just
handle it from there thanks again all and I hope my question help replace
whatever is missing from you life. Enjoy!!!
 
A

Alex Hunsley

jova said:
thanks for all ur

It's "your"...
help people that I thought it was a board

Noop, it's a newsgroup. This is Usenet. You may be posting to it via
google groups, but it's usenet.
that would try
to help others but, it seems like to make fun of others than actually
helping.

And what was wrong with the answer that Bjorn posted?
You obviously haven't spent any time reading any other posts if you
think that no-one gets any help.
Get over your sense of drive-by entitlement. You got a correct answer,
and these people aren't your slaves. If you're so disappointed I suggest
you demand the money back you paid for the advice.
And it seems you didn't bother to try google, which would have given you
the answer in a snap, if you'd searched for "java check if file exists"
or similar.

I hope your it helped fill the voids that is missing in your life.

One of the reasons some people didn't take your question too seriously
is that you're asking a question which is quite easily answerable by
using google. It sometimes looks lazy when people ask questions that
could have been answered well by google. (Isn't necessarily actually
lazy; sometimes the poster doesn't realise that google is so useful at
answering questions that way, or isn't sure of the search terms.)

Hint: anyone who posts questions that include "... and I tried to google
for phrase 'X' but I didn't turn up anything..." is regarded a little
more seriously, I find. It doesn't mean that you won't get help if you
don't saying you used google, though.
But I figure it out anyway by just trying to write to the file

Or you could use Bjorn's answer, which is correct.
and just
handle it from there thanks again all and I hope my question help replace
whatever is missing from you life. Enjoy!!!

Don't take umbrage because some people had a little fun. Remember,
you're not paying them for this.
 
J

jova

Alex Hunsley said:
It's "your"...


Noop, it's a newsgroup. This is Usenet. You may be posting to it via
google groups, but it's usenet.


And what was wrong with the answer that Bjorn posted?
You obviously haven't spent any time reading any other posts if you think
that no-one gets any help.
Get over your sense of drive-by entitlement. You got a correct answer, and
these people aren't your slaves. If you're so disappointed I suggest you
demand the money back you paid for the advice.
And it seems you didn't bother to try google, which would have given you
the answer in a snap, if you'd searched for "java check if file exists" or
similar.



One of the reasons some people didn't take your question too seriously is
that you're asking a question which is quite easily answerable by using
google. It sometimes looks lazy when people ask questions that could have
been answered well by google. (Isn't necessarily actually lazy; sometimes
the poster doesn't realise that google is so useful at answering questions
that way, or isn't sure of the search terms.)

Hint: anyone who posts questions that include "... and I tried to google
for phrase 'X' but I didn't turn up anything..." is regarded a little more
seriously, I find. It doesn't mean that you won't get help if you don't
saying you used google, though.


Or you could use Bjorn's answer, which is correct.


Don't take umbrage because some people had a little fun. Remember, you're
not paying them for this.

I did try it and I read the jdk help I seen no such method as Bjorn stated.
My question was plain and simple I ask if was there any method such as that
"I could check if the file existed". I didn't see an adequate reply to my
question. Then, I get such people trying to to make fun of my question.
I'm sorry for the incorrect spelling of "urs" but you knew exactly what I
meant or did you just like I may or may not understand what "I tried to
google" mean as well. Did you mean search the internet for solution to my
problem or something else because google is not in the dictionary sorry.
 
B

BlueOpal

In a multi-threaded environment, file operation or any other resource
access (which is out of the JVM) needs to be dealt very cautiously. Say
there are two threads and thread ONE returns TRUE for file.exists() and
just after that thread TWO deletes this file. Thread ONE would still
try to access this file resource which now does not exist on the file
system. So depending on the application we need to judge when to use
synchronization without paying the price of it in a single threaded
environment. In case you need to code for the most secure file
operation, let me know.

regards
Amit
Bangalore
 
A

Alex Hunsley

jova said:
I did try it

What, googling? What did you try googling for? "java check if file
exists" gets you the answer, anyway...
and I read the jdk help I seen no such method as Bjorn stated.

What exactly do you mean by the jdk help? (Link?)
If you read the Javadoc API for File you will see a method called exists().
Did you look for things like File in the java API docs? That's the usual
starting place (aside from google) if you're trying to find out if a
capability exists.
Check out http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html and
search for "exists()".
My question was plain and simple I ask if was there any method such as that
"I could check if the file existed". I didn't see an adequate reply to my
question.

Well, Bjorn gave you the correct answer. It was perfectly adequate. If
you didn't see it as adequate, it's not his fault.
Then, I get such people trying to to make fun of my question.

They weren't taking the mickey out of you particularly, just having some
fun.
I'm sorry for the incorrect spelling of "urs" but you knew exactly what I
meant

Yes, I knew what you meant. However, here are four reasons you should
use English in this group:

1) It's the standard language of the group. 'txt speak' isn't. This is
because:

2) It looks lazy. You're asking for help here, and yet using
abbreviations looks like you can't even be bothered to type out your
question and make yourself understood clearly. Is typing 'your' really
so much more effort than typing 'ur'?

3) Not everyone helping here has English as a first language. One guy
here recently commented that when people write 'ur' etc. he has to stop
and pronounce it to himself to work out what word the poster really
meant. If you want help, at least make it as simple as possible for
people to understand you.

4) By using text speak you stand out as someone who hasn't lurked in the
group much or read the java groups FAQ. That lack of effort on your
behalf can make other people less inclined to help you, even just
subconsciously!
or did you just like I may or may not understand what "I tried to
google" mean as well. Did you mean search the internet for solution to my
problem or something else because google is not in the dictionary sorry.

The bottom line on guildeines/rules is the standards of a group, not
just one simple rule like "You cannot post a word that is not in
dictionary X". That is why we have newsgroup FAQs. The FAQ for this
group was posted a couple of days ago.


(That aside, to 'google', as a verb, actually is in some dictionaries
now, AFAIK..)
 
T

Thomas Kellerer

Alex Hunsley wrote on 16.04.2006 10:27:
Yes, I knew what you meant. However, here are four reasons you should
use English in this group:

[skipped a lot of good reasons why to use proper english in a posting]

5) it's the minimum politeness one can ask for!

I usually don't even bother to answer if a posting is written in poor english.
Please note that poor english stemming from a non-native speaker is something
different and can easily be identified as such. But simple laziness is extremely
impolite IMHO. You wouldn't bother to speak to someone who does not even try to
utter complete words, wouldn't you?

Thomas
 
R

raisenero

I usually don't even bother to answer if a posting is written in poor english.

I agree. When people use NetSpeak to post, I usually skip their
message entirely. This is especially true in a programming newsgroup
or forum. It's hard enough interpreting code without piling on the
additional task of translating their question too. Some of the
greatest minds involved in technology are non-native English speakers
(e.g. Linus Torvalds, Bjarne Stroustrup, Satoru Iwata).

When communicating on the internet, it should always be assumed that
the receivers of our messages do not speak our language natively. Even
if the recipient does not speak the language, well-written text can be
translated by software. Translation services cannot translate
sms/txt/NetSpeak.

When asking a question, we want to eliminate as many barriers as
possible. The question sets the baseline for the quality of the
answers. It can safely be assumed that every answer will, at best, be
as clear and concise as the question was.
 
A

Andrew McDonagh

Thomas said:
Alex Hunsley wrote on 16.04.2006 10:27:
Yes, I knew what you meant. However, here are four reasons you should
use English in this group:

[skipped a lot of good reasons why to use proper english in a posting]

5) it's the minimum politeness one can ask for!

I usually don't even bother to answer if a posting is written in poor
english. Please note that poor english stemming from a non-native
speaker is something different and can easily be identified as such. But
simple laziness is extremely impolite IMHO. You wouldn't bother to speak
to someone who does not even try to utter complete words, wouldn't you?

Thomas

and yet you used 'IMHO'

go figure...
 
T

Thomas Kellerer

Andrew McDonagh wrote on 16.04.2006 12:08:
and yet you used 'IMHO'

go figure...

You got me there ;)

But then these are well established abbreviations in newsgroups. I'd assume that
they are just as "common" as i.e., e.g. or etc.

But to a certain extent, you are right.

Thomas
 
A

Alex Hunsley

Andrew said:
Thomas said:
Alex Hunsley wrote on 16.04.2006 10:27:
Yes, I knew what you meant. However, here are four reasons you should
use English in this group:

[skipped a lot of good reasons why to use proper english in a posting]

5) it's the minimum politeness one can ask for!

I usually don't even bother to answer if a posting is written in poor
english. Please note that poor english stemming from a non-native
speaker is something different and can easily be identified as such.
But simple laziness is extremely impolite IMHO. You wouldn't bother to
speak to someone who does not even try to utter complete words,
wouldn't you?

Thomas

and yet you used 'IMHO'

go figure...

IMHO and related items are acronyms, as opposed to lazy mispellings of
words like 'ur'. Not everyone knows what they mean, but they can soon
find out.
 
A

Andrew McDonagh

Alex said:
Andrew said:
Thomas said:
Alex Hunsley wrote on 16.04.2006 10:27:
Yes, I knew what you meant. However, here are four reasons you
should use English in this group:

[skipped a lot of good reasons why to use proper english in a posting]

5) it's the minimum politeness one can ask for!

I usually don't even bother to answer if a posting is written in poor
english. Please note that poor english stemming from a non-native
speaker is something different and can easily be identified as such.
But simple laziness is extremely impolite IMHO. You wouldn't bother
to speak to someone who does not even try to utter complete words,
wouldn't you?

Thomas

and yet you used 'IMHO'

go figure...

IMHO and related items are acronyms, as opposed to lazy mispellings of
words like 'ur'.

sure...

and words like 'ur' are abbreviations.

Both are perfectly legal English language constructs.
Not everyone knows what they mean, but they can soon
find out.

And the same can b said about abbreviations.

The beauty of Abbreviations is that their true meaning can often be
discovered by the context or even their placement within the sentence.

This allows us to create nu 1s on the fly.

Acronyms however, don't support this.

Something that makes me smile...

'GSM SMS messages'

or expanded to its proper form because acronyms are lazy....

'Global System for Mobile communication Simple Messaging System messages'

Both are such a 'mouthful' that they have been shortened to :

Text Message
Texts
txts
(and others)


The text part is funny - as its implied in the SMS part of the sentence
above. But because technology moved on and we got MultiMedia Messaging,
we introduced the 'text' to differentiate.
 
T

Thomas Hawtin

jova said:
Thanks this seems helpful. Is the exist() method new for the 1.5 jdk
because I have an old reference book that I was looking at, the book is
based on the1.3 Jdk and I do not see the method in this book or maybe I just
overlooked it. Thanks anyway!!!

It was in JDK 1.00. What is your book about? May I suggest using the API
documentation (a.k.a. JavaDocs).

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top