15

Can you create a File object in Java without saving to hard disk?

All the constructors for java.io.File seem to take info about a location on disk.

3
  • 5
    What do you want to do with the file? Do you actually want a stream? Commented Oct 16, 2013 at 20:28
  • Yeah I guess I just need a stream. But my main goal is persist the file object in a DB4O database as well as on disk, as a .groovy file. Commented Oct 17, 2013 at 0:48
  • So... You do want to save it to the disk. Why can't you just create a string and write it to the dB and the file? I'm confused regarding the actual issue. Commented Oct 17, 2013 at 0:57

2 Answers 2

13

A File object is not a file. It is a path to a location on disk with some utility methods. This location need not exist to have a valid file. For instance, you can do File#createNewFile to create a physical file after creating a File object that would point to that pathname on disk.

Use URL and related classes to read remotely, or some sort of java.nio.ByteBuffer to store file data in memory.

Sign up to request clarification or add additional context in comments.

Comments

3

Even if you create a file object, the file itself does not need to exist on disk.

When constructing the java object, you need to specify a location of the file absolute or relative to the current directory, but the file itself does not need to exist on disk.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.