6

I have instance FileOutputStream and I want to write file.

after execution my code I can't find file on my filesystem.

maybe FileOutputStream have method that I will know where it writes?

  • possible duplicate of [Get file name from FileOutputStream](http://stackoverflow.com/questions/4930111/get-file-name-from-fileoutputstream) – Behe Oct 03 '13 at 11:38
  • 3
    If you did not give an absolute path, it will be relative to the current working directory. If you don't know where that is, use an absolute path, or do a `System.out.println(new File(".").getAbsolutePath())`; – Thilo Oct 03 '13 at 11:40

5 Answers5

10

You decide where the file will be located, when you call constructor.

new FileOutputStream("path/to/my/file.txt");

There are a few similiar constructors. You can pass for example File or FileDescriptor parameter too. Just read Java API Doc.

http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html

Patryk Dobrowolski
  • 1,565
  • 2
  • 13
  • 29
2

When you created the FileOutputStream instance, you would have given either File object or a String path(absolute path with file name) or a FileDescriptor object. That path is where your file would be placed.

Have a look at the various constructors of FileOutputStream and check which was the one you had used.

Rahul
  • 44,383
  • 11
  • 84
  • 103
1
FileOutputStream(File file)
Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(File file, boolean append)
Creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(FileDescriptor fdObj)
Creates a file output stream to write to the specified file descriptor, which represents an existing connection to an actual file in the file system.
FileOutputStream(String name)
Creates a file output stream to write to the file with the specified name.
FileOutputStream(String name, boolean append)
Creates a file output stream to write to the file with the specified name.

All overloaded constructors take filename. if file does not exist in the absolute path provided a new one is created. If no absolute path is provided then file will be crated in current directory.

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
1

I just started working with Java on a RESTful service project. I have written to a file with FileOutputStream and couldn't find the file. My OS is Windows, and I am working with Eclipse. I finally found the file where the "eclipse.exe" is located on my computer. It looks like the Java class stored the file there if I did not provide an absolute path. It could be different in your case. This is such an old post but felt like replying as I see some posts asking similar questions even now.

Raj006
  • 562
  • 4
  • 18
0

The contstructor of the FileOutputStream object takes different parameters. One of them is a string of the path to your file. Another is a File object, in that case, you defined the path to your file when you created that File object.