Java中的文件路径

news/2024/9/12 16:51:01

通常情况下,在Java项目中,我们使用的路径都是在拿到类加载路径后,根据相对位置,使用

FilePathTest.class.getResourceAsStream(relativePath);拿到文件。
今天小生不使用classPath,而是直接去使用相对路径来试验。
小生的work space路径是 E:\workspace\springMVCStudy,在该work sapce下创建Java Project,目录如下。
1. 拿到new File("")所对应的绝对路径。
在MyUrlDemo类中,通过
File directory = new File("");
String courseFile = directory.getCanonicalPath();
System.out.println("项目根路径:" + courseFile);
我们直接拿到的是项目根路径。
结果是:::E:\workspace\springMVCStudy\testFilePath
2. test1.txt的路径
由于test1.txt位于项目的根路径下,我们可以直接使用如下方式拿到。
File file1 = new File("test1.txt");
3. test2.txt的路径
由于test2.txt位于src下,照猫画虎
File file2 = new File("src\\test2.txt");
4. test3.txt的路径
由于test3.txt位于src下,再次照猫画虎
File file2 = new File("src\\com\\rock\\testFilePath\\test3.txt");
这样我们拿到了3个文件,但是这样并不保险。因为在开发环境中,我们的文件结构是这样的,但是在测试环境和生产环境中,
我们的代码都是编译之后才才去运行。
在编译之后,上面的test1.txt不能被打到jar包中,也就是说,就不能再通过上面的方法访问test1.txt了。
对于test2.txt和test3.txt,也不能通过上述方法访问,这是因为编译之后就没有src目录了。
以下两种方法可以查看对应的路径,根据该路径可以拼出相对路径,然后使用相应的getResourceAsStream()得到文件的输入流。
1. 获取类的加载路径
当前类的加载路径::this.getClass().getResource("").getPath() 或者MyURLDemo.class.getResource("").getPath()
类加载的根路径::: this.getClass().getResource("/").getPath()
2. 获取类加载器的路径
this.getClass().getClassLoader().getResource("").getPath();  结果等同于类加载的根路径
附测试代码:
package com.rock.test.filePath;import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;public class FilePathTest {public static void main(String[] args) {//使用文件相对路径File file1 = new File("test1.txt");System.out.println("test1.txt exists " + file1.exists());File file2 = new File("src\\test2.txt");System.out.println("test2.txt exists " + file2.exists());File file3 = new File("src\\com\\rock\\test\\filePath\\test3.txt");System.out.println("test3.txt exists " + file3.exists());InputStream stream = FilePathTest.class.getResourceAsStream("/test2.txt");
//        InputStream stream = FilePathTest.class.getResourceAsStream("test3.txt");
//        InputStream stream = FilePathTest.class.getClassLoader().getResourceAsStream("test2.txt");BufferedReader reader = new BufferedReader(new InputStreamReader(stream));String line = null;try {while((line = reader.readLine()) != null) {System.out.println(line);}} catch (IOException e) {e.printStackTrace();}//使用类加载路径获取test2.txtFile file2ByLoard = new File(FilePathTest.class.getResource("/").getPath() + "\\test2.txt");System.out.println("test2.txt exists by load " + file2ByLoard.exists());File file2ByLoarder = new File(FilePathTest.class.getClassLoader().getResource("").getPath() + "\\test2.txt");System.out.println("test2.txt exists by loader " + file2ByLoarder.exists());File file2ByClasspath = new File(System.getProperty("java.class.path") + "\\test2.txt");System.out.println("test2.txt exists by Classpath " + file2ByClasspath.exists());}}

 

转载于:https://www.cnblogs.com/scottwang/p/4450938.html


http://lihuaxi.xjx100.cn/news/272506.html

相关文章

转:入侵网站必备-sql server

来源:http://www.bitscn.com/plus/view.php?aid28692 1.判断有无注入点 ; and 11 and 12 2.猜表一般的表的名称无非是admin adminuser user pass password 等.. and 0(select count(*) from *) and 0(select count(*) from admin) ---判断是否存在admin这张表 3.猜…

85.4% mIOU!NVIDIA:使用多尺度注意力进行语义分割,代码已开源!

点击上方“小白学视觉”,选择加"星标"或“置顶”重磅干货,第一时间送达下载论文PDF和源代码:链接:https://pan.baidu.com/s/17oy5JBnmDDOtKasfPrWNiQ 提取码:lk5z导读来自NVIDIA的SOTA语义分割文章&#xff…

Python赋值运算符(入门必读)

赋值运算符用来把右侧的值传递给左侧的变量(或者常量);可以直接将右侧的值交给左侧的变量,也可以进行某些运算后再交给左侧的变量,比如加减乘除、函数调用、逻辑运算等。 [Python] 中最基本的赋值运算符是等号&#x…

Linux 4.18 内核新补丁移除了Lustre 文件系统

2019独角兽企业重金招聘Python工程师标准>>> 在 Linux 4.18 的维护周期中,内核暂存区得到了超过一千个的补丁,共有168000行新代码出现,同时有227000行代码被删除。 为了使内核暂存区变得更轻,Lustre 文件系统在这次变更…

Java 程序员如何使用 Shiro 框架

点击上方“方志朋”,选择“设为星标”回复”666“获取新整理的面试文章作者:冷豪来自:www.cnblogs.com/learnhow/p/5694876.html一、架构要学习如何使用Shiro必须先从它的架构谈起,作为一款安全框架Shiro的设计相当精妙。Shiro的应…

简单粗暴理解与实现机器学习之逻辑回归:逻辑回归介绍、应用场景、原理、损失以及优化...

作者 | 汪雯琦责编 | Carol来源 | CSDN 博客出品 | AI科技大本营(ID:rgznai100)学习目标知道逻辑回归的损失函数知道逻辑回归的优化方法知道sigmoid函数知道逻辑回归的应用场景应用LogisticRegression实现逻辑回归预测知道精确率、召回率指标的区别知道如…

怪我不懂你

坐了很久的公交去买火车票。嗯,决定了,去武汉。 其实这次真的不确定是不是可以开心着回来,但是,总是这样我会死掉的。就算是死,我也想死的明白一点,死的瞑目。等不了了。 写下上面的文字的时候我已经从武汉…

再见吧,996!程序员开源考公指南获高赞:三人已成功上岸

点击上方“视学算法”,选择加"星标"或“置顶”重磅干货,第一时间送达整理 | 钰莹转载自公众号:AI前线近年来,互联网公司 996 和职场 PUA 现象见怪不怪,加之疫情影响下的工作不稳定,很多程序员考虑…