2

I have a question regarding to the Java File class. When I create a File instance, for example,

File aFile = new File(path);

Where does the instance aFile store in the computer? Or it stores in JVM? I mean is there a temp file stored in the local disk?

If I have an InputStream instance, and write it to a file by using OutputSteam, for example

  File aFile = new File("test.txt");

  OutputStream anOutputStream = new FileOutputStream(aFile);

  byte aBuffer[] = new byte[1024];

  while( ( iLength = anInputStream.read( aBuffer ) ) > 0)
  {
    anOutputStream.write( aBuffer, 0, iLength);
  }

Now where does the file test.txt store?

Thanks in advance!

3 Answers 3

6

A File object isn't a real file at all - it's really just a filename/location, and methods which hook into the file system to check whether or not the file really exists etc. There's no content directly associated with the File instance - it's not like it's a virtual in-memory file, for example. The instance itself is just an object in memory like any other object.

Creating a File instance on its own does nothing to the file system.

When you create a FileOutputStream, however, that does affect whatever file system you're writing to. The File instance is relatively irrelevant though - you'd get the same effect from:

OutputStream anOutputStream = new FileOutputStream("test.txt");
Sign up to request clarification or add additional context in comments.

Comments

1

It will write the file where you specify it with path arguement.

In your case, it will write it in the directory where you run your java class.

If you specify /test/myproject/myfile.txt

it will go in /test/myproject/myfile.txt

Comments

0

If you don't provide a path, it is in the current directory (ie: the directory where java.exe is executed from.) If you provide a full path, it is stored there.

Regardless, it is always stored in the filesystem, not in JVM memory.

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.