Java FileOutputStream 默认创建路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16469549/
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
Java FileOutputStream Default Creation Path
提问by Jay Zhang
Let's say I have below code:
假设我有以下代码:
String fileName = "name.txt";
FileOutputStream fileOut = new FileOutputStream(fileName);
wb.write(fileOut);
This way, the file will be created under bin folder of the project.
这样,该文件将在项目的 bin 文件夹下创建。
However, if I specific fileName at a whole path:
但是,如果我在整个路径中指定 fileName:
String fileName = "c:/temp/name.txt";
this file will be created at c:\temp folder.
该文件将在 c:\temp 文件夹中创建。
Am Correct? And Why this happen, how FileOutputStream work?
正确吗?为什么会发生这种情况,FileOutputStream 是如何工作的?
回答by Wormbo
If you don't specify an absolute path, e.g. if you only specify the file name, then your program or the operating system somehow needs to figure out, where to find that file. For that reason a running program always has a working directory. That happens to be the folder you start it from, by default.
如果您不指定绝对路径,例如,如果您只指定文件名,那么您的程序或操作系统需要以某种方式弄清楚,在哪里可以找到该文件。出于这个原因,一个正在运行的程序总是有一个工作目录。默认情况下,这恰好是您启动它的文件夹。
回答by A4L
It's not about how FileOutputStream
works, it's about the path that the operating system assigns to a process when it starts it
这不是关于如何FileOutputStream
工作,而是关于操作系统在启动进程时分配给它的路径
This path is called current working directory. From that directory all relative paths
are calculated. A simple file name is a relative path (to the current working directory).
此路径称为当前工作目录。从该目录relative paths
中计算出所有内容。一个简单的文件名是一个相对路径(到当前工作目录)。
If you specify an absolute path
then this path is used to create the file.
如果您指定,absolute path
则此路径用于创建文件。
You can read more about paths on this wiki page.
您可以在此 wiki 页面上阅读有关路径的更多信息。
回答by AlexR
Unless you specify an absolute path, the path is relative to current working directory.
除非您指定绝对路径,否则该路径是相对于当前工作目录的。
If your current working directory is a bin folder in your project, it will be created there.
如果您当前的工作目录是项目中的 bin 文件夹,它将在那里创建。
回答by NilsH
If you only specify the filename, it will be created in the current working directory. If you do specify an absolute path, it will of course be created at that path.
如果您只指定文件名,它将在当前工作目录中创建。如果您确实指定了绝对路径,它当然会在该路径上创建。
回答by Mateusz
It's all about relative and absolute directories. Let's say that you specif path foo/bar
. It'll create a file bar
in foo
directory of your working folder. The same applies for ../foo/bar
it'll create a bar
file in foo
directory in the folder above the working dir. However, if you type C:\\Documents\ and\ Settings\User\Desktop\bar
(or /home/user/Desktop/bar
), it'll create a bar
on your desktop. For more info take a look here.
这都是关于相对和绝对目录的。假设您指定了 path foo/bar
。它将bar
在foo
您的工作文件夹的目录中创建一个文件。这同样适用于../foo/bar
它会在工作目录上方的文件夹bar
中的foo
目录中创建一个文件。但是,如果您键入C:\\Documents\ and\ Settings\User\Desktop\bar
(或/home/user/Desktop/bar
),它会bar
在您的桌面上创建一个。有关更多信息,请查看此处。