Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何知道FileOutputStream将在哪里写入文件?_Java_File_Stream - Fatal编程技术网

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

Java 如何知道FileOutputStream将在哪里写入文件?,java,file,stream,Java,File,Stream,我有instanceFileOutputStream,我想写这个文件 执行代码后,我在文件系统中找不到文件 也许FileOutputStream有一种方法可以让我知道它写在哪里?当您调用构造函数时,您可以决定文件的位置 new FileOutputStream("path/to/my/file.txt"); 有一些类似的构造函数。您也可以传递例如File或FileDescriptor参数。只需阅读JavaAPI文档 当您创建文件输出流实例时,您可能会给出文件对象或字符串路径(带有文件名的绝对路

我有instance
FileOutputStream
,我想写这个文件

执行代码后,我在文件系统中找不到文件


也许
FileOutputStream
有一种方法可以让我知道它写在哪里?

当您调用构造函数时,您可以决定文件的位置

new FileOutputStream("path/to/my/file.txt");
有一些类似的构造函数。您也可以传递例如File或FileDescriptor参数。只需阅读JavaAPI文档


当您创建
文件输出流
实例时,您可能会给出
文件
对象或
字符串路径
(带有文件名的绝对路径)或
文件描述符
对象。该路径是放置文件的位置


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

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

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.

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

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

如果你没有给出一个绝对路径,它可能会与当前的工作目录相对。如果您不知道它在哪里,请使用绝对路径,或者执行
System.out.println(新文件(“.”.getAbsolutePath())
@是的,我已经在我的回答中提到过!)一开始你没有错过。我稍后编辑了它,但就在你发表评论之前!:)无论如何,建设性的评论总是受欢迎的。或者是带有相对路径的字符串。