> > 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).
<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!
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.
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 !!!
> 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.
> 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."
> 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.
~
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.
> 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)?