How to know where FileOutputStream will write file?
Asked Answered
A

5

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?

Attalanta answered 3/10, 2013 at 11:35 Comment(2)
possible duplicate of Get file name from FileOutputStreamInput
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());Bluebird
C
11

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

Countrydance answered 3/10, 2013 at 11:36 Comment(0)
B
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.

Bambino answered 3/10, 2013 at 11:38 Comment(3)
@MichaëlBenjaminSaerens - Yeah I already mentioned in that my answer! :)Bambino
You didn't miss it initially. I edited it a bit later, but just before you posted your comment!:) Anyways, constructive comments are always welcome.Bambino
Or a String with a relative path.Anxious
F
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.

Fleabitten answered 3/10, 2013 at 11:39 Comment(0)
M
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.

Mehta answered 9/8, 2018 at 3:15 Comment(0)
L
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.

Leoni answered 3/10, 2013 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.