Java:如何在 C:\ 驱动器中创建文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19801918/
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: How to create a file in the C:\ drive
提问by TheProjectCodex
I am trying to get the plain C:\
path with java to store files in. I am having issues trying to figure this out as other resources didn't tell me, but they were using other paths. I know how to get the user's home like:
我正在尝试C:\
使用 java获取普通路径来存储文件。我在尝试解决这个问题时遇到了问题,因为其他资源没有告诉我,但他们正在使用其他路径。我知道如何让用户的家像:
File f = new File(System.getProperty("user.home");
File f = new File(System.getProperty("user.home");
but that only creates a new file in the user's main folder C:\Users\<username>\
. How do I just create a file within the C:\
directly in the drive?
但这只会在用户的主文件夹中创建一个新文件C:\Users\<username>\
。如何C:\
直接在驱动器中创建文件?
采纳答案by Kylar
Create a file object that points to exactly where you want.
创建一个指向您想要的确切位置的文件对象。
File f = new File("C:\");
Note that the '\' character needs to be escaped, thus the "\\".
请注意,'\' 字符需要转义,因此需要转义“\\”。
To create a file there, you could do something like this:
要在那里创建文件,您可以执行以下操作:
File f = new File("C:\myfile.txt");