java 获取 文件_JAVA获取文件的几种常用方式

1、user.dir

System.out.println(System.getProperty("user.dir"));

此方获取的是运行的路

比如

1、

33cef06f7dea93c12352e741a9b0de3a.png

2、如果在eclipse上运行则是eclipse运行文件同级

5dbc7e0361613ef7253e56c52ae7def2.png

3、tomcat则在

737da3f0b77744d90ce1ea880dcbcf82.png

4、File file = new File("log4j.properties");

这里的log4j.properties也是在以上类型的目录下查找

5、FileInputStreaminput = newFileInputStream("log4j.properties");

与File一样,在运行路径上找文件

2、获取绝对路径

方法 String in = LogUtil.class.getResource("/").getPath();

或者:this.getClass().getClassLoader().getResource("/").getPath()

结果:

/E:/temp/springmvc/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/rentBook/WEB-INF/classes/

LogUtil.class.getResource("/").getPath(); 加个”/”就会打印classes下的绝对路径

LogUtil.class.getResource("").getPath(); 就会显示classes + 包名

Tomcat打印如下:

9d7f0e9a02813a0107c4533c1d136531.png

WEB工程

this.getServletContext().getRealPath("/")

返回:

工程的绝对路径/返回的也称为“根”

比如工程:

Book

|_WEB-INF

|_xx.properties

返回d:\Book\

3、相对路径:

InputStream input = LogUtil.class.getResourceAsStream("/log4j.properties");

读取的是src/log4j.properties 下的文件

InputStream input = LogUtil.class.getResourceAsStream("log4j.properties");

读取的是 与LogUtil同级的log4j.properties

第二种:

getServletConfig().getServletContext()

.getResourceAsStream("report1.jasper")

访问的是根路径

Book

|_WEB-INF

|_xx.properties

|_report1.jasper

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 获取配置文件信息有很多方法,常用的有以下几种: 1. 使用 java.util.Properties 类。Properties 类可以用来读取 Java 的配置文件,通常是以 .properties 为后缀的文件。使用 Properties 类可以方便地读取配置文件的信息。例如: ```java import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class Main { public static void main(String[] args) throws IOException { Properties prop = new Properties(); prop.load(new FileInputStream("config.properties")); String name = prop.getProperty("name"); int age = Integer.parseInt(prop.getProperty("age")); } } ``` 2. 使用 java.util.ResourceBundle 类。ResourceBundle 类可以用来读取多种语言的配置文件,通常是以 .properties 为后缀的文件。例如: ```java import java.util.ResourceBundle; public class Main { public static void main(String[] args) { ResourceBundle rb = ResourceBundle.getBundle("config"); String name = rb.getString("name"); int age = Integer.parseInt(rb.getString("age")); } } ``` 3. 使用 java.util.prefs.Preferences 类。Preferences 类可以用来读取操作系统的配置信息,例如 Windows 的注册表。例如: ```java import java.util.prefs.Preferences; public class Main { public static void main(String[] args) { Preferences prefs = Preferences.userNodeForPackage(Main.class); String name = prefs.get("name", ""); int age = prefs.getInt("age", 0); } } ``` 4. 使用 java.util.Scanner 类。Scanner 类可以用来读取任意类型的文件,包括配置文件。例如: ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(new

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值