Java 如何知道 FileOutputStream 将在哪里写入文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19158169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:35:41  来源:igfitidea点击:

How to know where FileOutputStream will write file?

javafilestream

提问by

I have instance FileOutputStreamand I want to write file.

我有实例FileOutputStream,我想写文件。

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

执行我的代码后,我在我的文件系统上找不到文件。

maybe FileOutputStreamhave method that I will know where it writes?

也许FileOutputStream有方法我会知道它写在哪里?

回答by Patryk Dobrowolski

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.

有一些类似的构造函数。您也可以传递例如 File 或 FileDescriptor 参数。只需阅读 Java API 文档。

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

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

回答by SudoRahul

When you created the FileOutputStreaminstance, you would have given either Fileobject or a String path(absolute path with file name) or a FileDescriptorobject. That path is where your file would be placed.

创建FileOutputStream实例时,您将提供File对象或String path(带有文件名的绝对路径)或FileDescriptor对象。该路径是放置文件的位置。

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

查看FileOutputStream的各种构造函数,并检查您使用的是哪个构造函数。

回答by Micha?l Benjamin Saerens

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.

FileOutputStream 对象的构造函数采用不同的参数。其中之一是文件路径的字符串。另一个是 File 对象,在这种情况下,您在创建该 File 对象时定义了文件的路径。

回答by Aniket Thakur

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.

所有重载的构造函数都采用文件名。如果文件在绝对路径中不存在,则提供一个新文件。如果未提供绝对路径,则文件将在当前目录中创建。

回答by Raj006

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.

我刚刚开始在一个 RESTful 服务项目中使用 Java。我已使用 FileOutputStream 写入文件,但找不到该文件。我的操作系统是 Windows,我正在使用 Eclipse。我终于在我的电脑上找到了“eclipse.exe”所在的文件。如果我没有提供绝对路径,看起来 Java 类将文件存储在那里。你的情况可能会有所不同。这是一个如此古老的帖子,但我还是想回复,因为我现在看到一些帖子问类似的问题。