string function

Hello allTo get the file name from a String path, I did:String path = "abac\vafda\dafads\myFile.txt";int lastIndex = path.lastIndexOf("\"); path = path.substring(lastIndex + 1);Is there better way to do thisThanks
[262 byte] By [suhua] at [2007-11-27 8:29:16]
# 1
File file = new File("abac/vafda/dafads/myFile.txt");System.out.println(file.getName()); // myFile.txt~
yawmarka at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 2
And the lesson is: Look in the API to see what methods the class has.
floundera at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 3
Thanks but I got the path String from a database
suhua at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 4
And the lesson is: Look in the API to see what classes are available and the methods those class have.
floundera at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 5
And the result is : no better way
suhua at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 6
> And the result is : no better wayHuh?Did you read reply #1. It shows you the best way to do it.
floundera at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 7
> File file = new File("abac/vafda/dafads/myFile.txt");If I use this way, I have to create a variable File, it takes memory ?
suhua at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 8
ROFL!And you really think that matters. Your program won't (or at least it shouldn't) crash if you create an object. If it does then you have more things to worry about than the creation of a single object.
floundera at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 9
String path = "abac\vafda\dafads\myFile.txt";int lastIndex = path.lastIndexOf("\"); Also, are you under the impression that the above code doesn't use memory?
floundera at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 10

> > File file = new

> File("abac/vafda/dafads/myFile.txt");

>

> If I use this way, I have to create a variable File,

> it takes memory ?

perhaps you think that creating this variable fills memory with all the contents of myFile? I don't think this is so unless you open the file. I could be wrong though (again, it's happened before).

petes1234a at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 11
Thank you for your helpI think that create an "int" is better then create a "File" object (as peter suggestion)I am sorry if I am wrong
suhua at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 12

<shakes head>

What you are saying is like trying to smash an extremely large boulder with an ice pick rather than a sledge hammer because it will take less energy to use the ice pick. You totally ignore the fact that you will probably die before achieving the task with the ice pick. Use the right tool for the job!

floundera at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 13

Creating a File object will, indeed, use a little bit of memory on the heap, to store the object.

But so what. Java is an object-oriented language. It's all about objects. You're supposed to create objects. The heap memory used by the File object will be minuscule. It will be easily handled.

Your program won't be better by avoiding creating objects. In fact it will be worse, as your code will make horrible contortions to avoid doing what it should be doing in the first place.

Use the right tool for the job and all will be well.

paulcwa at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 14
> Thank you for your help> > I think that create an "int" is better then create a> "File" object (as peter suggestion)no.> I am sorry if I am wrongyes, you are wrong.
petes1234a at 2007-7-12 20:19:30 > top of Java-index,Java Essentials,New To Java...
# 15
I just can't understand why people come here asking for advice from people with a lot more experience than them and when they get that advice go "Screw you guys! I know better and I will do it my way"
floundera at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 16

Hello flounder

============

>I just can't understand why people come here asking for advice from people with a lot more experience than them and when they get that advice go "Screw you guys! I know better and I will do it my way"

============

I also donot understand why you wrote like this.

I donot know which way is better, so I wrote my opinion. By showing my opinion and someone like you to correct it, I know why I am wrong !!!

suhua at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 17
I wrote that because you seem intent on continuing to use your method despite being told a better way to do it.
floundera at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 18
No, I only give my opinionI think that I create a File object when I need to open, read file from disk. I donot know that I can create a file object only for getting the file nameThanks all
suhua at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 19
And we gave our opinions as to why you were wrong!Message was edited by: flounder
floundera at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 20
I aslo gave my opinion in reply 15 and you seem to be questioning it. So it's ok for you to post your opinions but it is not ok for me to post mine?
floundera at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 21
Yep, sir :)
suhua at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 22
Bite my shiny scaly arse!
floundera at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 23
Here is the reply 15 of 22>just can't understand why people come here asking for advice from people with a lot more experience than them and when they get that advice go "Screw you guys! I know better and I will do it my way"
suhua at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 24
I know, I wrote it. Do you think I can't remember what it says?
floundera at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 25
OKbut your opinion in this case is wrong as I never - in my posts implying that:...I know better and I will do it my way"
suhua at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 26
Whether you believe I am right or wrong does not matter, it is my opiniion and I am entitled to it. You cannot change that, you cannot prevent me from stating that. If you don't like it, you are free to leave Just like I am free to post what I like!
floundera at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 27
I suggest we all just drop this.....
petes1234a at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 28

> OK

>

> but your opinion in this case is wrong as I never -

> in my posts implying that:

>

> ...I know better and I will do it my way"

then just state that you now know the correct way as suggested above by flounder, give thanks, and lets be on our way.

petes1234a at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 29
BTW you did imply that.You were given a perfectly good example of a better way to do it. Rather than saying "Thankyou" you mutter something about memory usage. That implied to me you would rather not do it that way.
floundera at 2007-7-21 22:41:25 > top of Java-index,Java Essentials,New To Java...
# 30
OK,I think I already thanksHowever, again: thanks all
suhua at 2007-7-21 22:41:30 > top of Java-index,Java Essentials,New To Java...
# 31

> Thank you for your help

>

> I think that create an "int" is better then create a

> "File" object (as peter suggestion)

>

> I am sorry if I am wrong

In the context of any halfway reasonable or normal program, that's like saying "It's better to spend $13,095.05 than $13,095.08."

jverda at 2007-7-21 22:41:30 > top of Java-index,Java Essentials,New To Java...
# 32

> perhaps you think that creating this variable fills memory with all the contents of myFile? I don't think this is so unless you open the file. I could be wrong though (again, it's happened before).

You're not wrong.

public class File

extends Object

implements Serializable, Comparable<File>An abstract representation of file and directory pathnames.

~

yawmarka at 2007-7-21 22:41:30 > top of Java-index,Java Essentials,New To Java...
# 33

You seem to be making the newbie mistake of thinking that a File object holds the whole file on disk, and that creating a File object will use as much memory as the file holds.

As yawmark has showed you, the File object represents the path to the file. The file doesn't even need to exist.

Think of a File object as representing the line in a directory listing for that file.

That's closer to the truth.

paulcwa at 2007-7-21 22:41:30 > top of Java-index,Java Essentials,New To Java...
# 34

> Thank you for your help

>

> I think that create an "int" is better then create a

> "File" object (as peter suggestion)

>

> I am sorry if I am wrong

It is a matter of degree, but yes you are wrong.

You would be right if you have

1. Specifically determined that there is a specific reason this app has special needs.

2. Determined that those special needs apply to this specific code.

3. Determined that the code that you are using instead actually is "better" (uses less memory, less objects, etc.)

Have you looked at the code for File and indexOf to determine the last one?

If no then you haven't done 3.

Have you done 2.

What needs does this app have that require this (1 above)?

jschella at 2007-7-21 22:41:30 > top of Java-index,Java Essentials,New To Java...