Fundamental question about files
Hi everyone,
I have a pretty fundamental question about Java. When I instantiate a new file like so,
File stuff = new File(/home/sal/path/to/big/file.jpg);
Is the entire file thrown on the heap, or is only the 'stuff' object created, which has a pointer to the file on my disc drive. I'll try to elaborate a little better: once that command is given, is the entire 'file.jpg' sitting on RAM, or is there simply a pointer to it.
Any feedback would be greatly appreciated!
Re: Fundamental question about files
If you haven't read the file in one way or another, then the contents of the file isn't in memory.
Re: Fundamental question about files
Thanks for the quick reply doWhile!
I have a few more questions; what constitutes reading the file? If I run a command like file.length to retrieve the amount of bytes the file is taking, does that still keep it off the heap? (So the file isn't called in RAM.) Is reading only done when I start a Reader object? (Like BufferedReader or the like.)
Finally, if the file is placed on the heap once it's read, how do I take it off? Can I run the command file.close to remove it from the heap?
Re: Fundamental question about files
Quote:
Is reading only done when I start a Reader object?
Reading a file into an Object in memory results in an object like any other object in the JVM - if you read a File (I should stress explicitly read - the JVM does not read the file for you) and keep references to the contents in memory, it will stay in memory. If the reference(s) looses scope or in any other ways looses its reference(s), they may be garbage collected. Calling methods like File.length would be a complete waste of resources - and even duplicate information - should they required the JVM to read the whole file into memory.
Re: Fundamental question about files
Thank you for the responses, doWhile. This clears things up!