java 是否可以将字符串变量分配给文件的绝对路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16891417/
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
Is it possible to assign a String variable the absolute path of a File?
提问by Ellipsis
I am wondering if it is possible to assign a String Variable the path of the file? If Yes, then is it possible to update the File Dynamically? I am trying to create Files dynamically (which I am able to do so), but I want to link these dynamically created files to a String variable. Please help. Thanks in advance.
我想知道是否可以为文件路径分配一个字符串变量?如果是,那么是否可以动态更新文件?我正在尝试动态创建文件(我能够这样做),但我想将这些动态创建的文件链接到 String 变量。请帮忙。提前致谢。
File dir = new File("Data");
if(!dir.exists()){
dir.mkdir();
}
String filename = "file1";
File tagfile = new File(dir, filename+".txt");
if(!tagfile.exists()){
tagfile.createNewFile();
}
System.out.println("Path : " +tagfile.getAbsolutePath());
回答by xagyg
String s = new File("xyz.txt").getAbsolutePath();
or
或者
String s = new File("xyz.txt").getCanonicalPath();
Both of the above assign (in my case) c:\dev\xyz.txt
to the string s
.
以上两个都分配(在我的情况下)c:\dev\xyz.txt
给 string s
。
回答by tgkprog
To get the full system path windows or linux
获取完整的系统路径 windows 或 linux
public static void main(String []args){
String path = "../p.txt";//works on windows or linux, assumes you are not in root folder
java.io.File pa1 = new java.io.File (path);
String s = null;
try {
s = pa1.getCanonicalFile().toString();
System.out.println("path " + s);
} catch (Exception e) {
System.out.println("bad path " + path);
e.printStackTrace();
}
Prints out full path like c:\projects\file\p.txt
打印出完整路径,如 c:\projects\file\p.txt
回答by Juned Ahsan
Here is the code to do that:
这是执行此操作的代码:
File file = new File("C:\testfolder\test.cfg");
String absolutePath = file.getAbsolutePath();
This is what javadoc says about the getAbsolutePath API:
这是 javadoc 关于 getAbsolutePath API 的说法:
getAbsolutePath
public String getAbsolutePath() Returns the absolute pathname string of this abstract pathname. If this abstract pathname is already absolute, then the pathname string is simply returned as if by the getPath() method. If this abstract pathname is the empty abstract pathname then the pathname string of the current user directory, which is named by the system property user.dir, is returned. Otherwise this pathname is resolved in a system-dependent way. On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory. On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory.
Returns: The absolute pathname string denoting the same file or directory as this abstract pathname
获取绝对路径
public String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串。如果这个抽象路径名已经是绝对路径名,那么路径名字符串就像由 getPath() 方法一样简单地返回。如果此抽象路径名是空抽象路径名,则返回由系统属性 user.dir 命名的当前用户目录的路径名字符串。否则,此路径名将以系统相关的方式解析。在 UNIX 系统上,相对路径名通过针对当前用户目录进行解析而成为绝对路径名。在 Microsoft Windows 系统上,相对路径名通过针对由路径名命名的驱动器的当前目录(如果有)进行解析而成为绝对路径名;如果不是,则针对当前用户目录进行解析。
返回: 表示与此抽象路径名相同的文件或目录的绝对路径名字符串