文件的三种加载方式(全网讲解最全,最严谨)
程序员文章站
2022-06-21 15:01:29
1.通过文件系统加载,也就是File类@Test public void testFileSystem() { //1.File-用户根目录 String property = System.getProperty("user.dir"); System.out.println("property:"+property);//property:G:\IdeaProjects\poxiao-cloud\poxiao-service\poxiao-sys...
1.通过文件系统加载,也就是File类
@Test
public void testFileSystem() {
//1.File-用户根目录
String property = System.getProperty("user.dir");
System.out.println("property:"+property);//property:G:\IdeaProjects\poxiao-cloud\poxiao-service\poxiao-system
//2.File-绝对路径
String name = new File("/banner.txt").getAbsolutePath();
System.out.println("name:"+name);//name:G:\banner.txt
Assert.assertNotNull(name);
String name0 = new File("banner.txt").getAbsolutePath();
System.out.println("name0:"+name0);//name0:G:\IdeaProjects\poxiao-cloud\poxiao-service\poxiao-system\banner.txt
Assert.assertNotNull(name0);
//3.File-相对路径
String name1 = new File("audio/explode.wav").getAbsolutePath();
System.out.println("name1:"+name1);//name1:G:\IdeaProjects\poxiao-cloud\poxiao-service\poxiao-system\audio\explode.wav
Assert.assertNotNull(name1);
//4.File-相对路径-验证绝对路径
String name2 = new File("/audio/explode.wav").getAbsolutePath();
System.out.println("name2:"+name2);//name2:G:\audio\explode.wav
Assert.assertNotNull(name2);
//5.File-相对路径-验证绝对路径
String name3 = new File("/audio/explode.wava").getAbsolutePath();
System.out.println("name3:"+name3);//name3:G:\audio\explode.wava
Assert.assertNotNull(name3);
//结论:
//使用file(文件系统)有相对路径和绝对路径
//相对路径:指的是System.getProperty("user.dir")(用户当前路径)下的路径,也就是当前项目名之下
//绝对路径:是指根目录下的路径,而根目录在window系统指c,d,e,f,在linux系统指/
}
结论:
- 使用file(文件系统)有相对路径和绝对路径
- 相对路径:指的是System.getProperty(“user.dir”)(用户当前路径)下的路径,也就是当前项目名之下
- 绝对路径:是指根目录下的路径,而根目录在window系统指c,d,e,f,在linux系统指/
2.使用class加载
@Test
public void testClassFile() throws IOException {
//1.class-绝对路径
InputStream inputStream = this.getClass().getResourceAsStream("/banner.txt");
byte[] bytes = new byte[1024];
inputStream.read(bytes);
System.out.println("bytes[0]:"+bytes[0]);
Assert.assertNotNull("为空",inputStream);//not null
//2.class-相对路径
InputStream inputStream2 = this.getClass().getResourceAsStream("banner.txt");
Assert.assertNull("不为空",inputStream2);//is null
//3.class-相对路径-""
String path = this.getClass().getResource("").getPath();
System.out.println("path:"+path);//path:/G:/IdeaProjects/poxiao-cloud/poxiao-service/poxiao-system/target/test-classes/com/poxiao/system/
//4.class-绝对路径-"/"
String path1 = this.getClass().getResource("/").getPath();
System.out.println("path1:"+path1);//path1:/G:/IdeaProjects/poxiao-cloud/poxiao-service/poxiao-system/target/test-classes/
//5.class-相对路径-"banner.txt"
String path2 = this.getClass().getResource("banner.txt").getPath();
System.out.println("path2:"+path2);//null point
//6.class-绝对路径-"/banner.txt"
String path3 = this.getClass().getResource("/banner.txt").getPath();
System.out.println("path3:"+path3);//path3:/G:/IdeaProjects/poxiao-cloud/poxiao-service/poxiao-system/target/classes/banner.txt
//结论:
//使用class加载有相对路径和绝对路径
//相对路径:指的是当前类所在的包下
//绝对路径:指的是当前类所在的根路径下,指的是classes或者test-classes
}
结论:
- 使用class加载有相对路径和绝对路径
- 相对路径:指的是当前类所在的包下
- 绝对路径:指的是当前类所在的根路径下,指的是classes或者test-classes
3.使用类加载器加载
@Test
public void testClassLoader() throws IOException {
//1.class-绝对路径
// InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("/banner.txt");
// byte[] bytes = new byte[1024];
// inputStream.read(bytes);//null point
// System.out.println("bytes[0]:"+bytes[0]);
// Assert.assertNotNull("为空",inputStream);
//2.class-相对路径
InputStream inputStream2 = this.getClass().getClassLoader().getResourceAsStream("banner.txt");
Assert.assertNotNull("为空",inputStream2);//not null
//3.class-相对路径-""
String path = this.getClass().getClassLoader().getResource("").getPath();
System.out.println("path:"+path);//path:/G:/IdeaProjects/poxiao-cloud/poxiao-service/poxiao-system/target/test-classes/
//4.class-绝对路径-"/"
// String path1 = this.getClass().getClassLoader().getResource("/").getPath();
// System.out.println("path1:"+path1);//is null
//5.class-相对路径-"banner.txt"
String path2 = this.getClass().getClassLoader().getResource("banner.txt").getPath();
System.out.println("path2:"+path2);//path2:/G:/IdeaProjects/poxiao-cloud/poxiao-service/poxiao-system/target/classes/banner.txt
//6.class-绝对路径-"/banner.txt"
// String path3 = this.getClass().getClassLoader().getResource("/banner.txt").getPath();
// System.out.println("path3:"+path3);//null point
//结论
//classLoador只有相对路径,这个和类加载器所处的位置有关系
//相对路径:相对于根路径,这个根路径指classes和test-classes的下面
}
结论:
- classLoador只有相对路径,这个和类加载器所处的位置有关系
- 相对路径:相对于根路径,这个根路径指classes和test-classes的下面
本文地址:https://blog.csdn.net/qq_40321119/article/details/109840103
上一篇: 机器学习与互联网搜索
下一篇: 别人还以为我和一个鸡在一起呢