Results 1 to 5 of 5
  1. #1
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default 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!

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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.

  3. #3
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default 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?

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Fundamental question about files

    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.

  5. #5
    salmontres is offline Member
    Join Date
    Dec 2011
    Posts
    30
    Rep Power
    0

    Default Re: Fundamental question about files

    Thank you for the responses, doWhile. This clears things up!

Similar Threads

  1. Compiler and files question
    By amro in forum New To Java
    Replies: 0
    Last Post: 10-31-2010, 06:08 PM
  2. fundamental question about Integer (for example)
    By kmansj in forum New To Java
    Replies: 10
    Last Post: 06-21-2010, 12:40 PM
  3. Question about ActionListeners and JAR files
    By Psyclone in forum AWT / Swing
    Replies: 6
    Last Post: 03-20-2010, 12:08 PM
  4. 2 Part question about cfg xml and dat files
    By Samgetsmoney in forum New To Java
    Replies: 0
    Last Post: 02-18-2009, 02:36 AM
  5. basic question about files
    By oregon in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 02:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •