java 路径
程序员文章站
2022-05-09 23:18:10
...
获取java路径的方法,类路径,工程路径,和创建文件
package com.pingan.ff.gw;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
public class PathTest {
@Test
public void pathTest() throws IOException {
System.out.println("user.dir:" + System.getProperty("user.dir"));
File directory = new File(".");
System.out.println("directory.getCanonicalPath():" + directory.getCanonicalPath());
System.out.println("directory.getAbsolutePath():"+ directory.getAbsolutePath());
System.out.println("this.getClass().getResource(\"/\").getPath():"+ this.getClass().getResource("/").getPath()+"../../pathtest");
String mockDir = this.getClass().getResource("/").getPath()+"../../pathtest/";
File testFile = new File(mockDir+"testfile.txt");
if(!testFile.exists()){
if(!testFile.getParentFile().exists()){
testFile.getParentFile().mkdirs();
}
testFile.createNewFile();
}
System.out.println("this.getClass().getResource(\"\").getPath():"+ this.getClass().getResource("").getPath());
System.out.println("this.getClass().getClassLoader().getResource(\"\"):"+ this.getClass().getClassLoader().getResource(""));
System.out.println("System.getProperty(\"java.class.path\"):" + System.getProperty("java.class.path"));
}
}
上一篇: 华为畅享9e直降200元:3+32GB到手仅599元
下一篇: QT无法获取exe所在的目录