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?
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?
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
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.
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.
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.
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.
© 2022 - 2024 — McMap. All rights reserved.
System.out.println(new File(".").getAbsolutePath())
; – Bluebird