Results 1 to 5 of 5
Thread: Fundamental question about files
- 12-07-2011, 08:59 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 30
- Rep Power
- 0
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!
- 12-07-2011, 09:39 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
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.
- 12-07-2011, 09:57 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 30
- Rep Power
- 0
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?
- 12-07-2011, 10:06 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Fundamental question about files
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.Is reading only done when I start a Reader object?
- 12-08-2011, 01:14 AM #5
Member
- Join Date
- Dec 2011
- Posts
- 30
- Rep Power
- 0
Similar Threads
-
Compiler and files question
By amro in forum New To JavaReplies: 0Last Post: 10-31-2010, 06:08 PM -
fundamental question about Integer (for example)
By kmansj in forum New To JavaReplies: 10Last Post: 06-21-2010, 12:40 PM -
Question about ActionListeners and JAR files
By Psyclone in forum AWT / SwingReplies: 6Last Post: 03-20-2010, 12:08 PM -
2 Part question about cfg xml and dat files
By Samgetsmoney in forum New To JavaReplies: 0Last Post: 02-18-2009, 02:36 AM -
basic question about files
By oregon in forum New To JavaReplies: 1Last Post: 08-05-2007, 02:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks