eclipse工程/文件目录,相对路径(真心的详细~)


雨伞 1. 获取系统根目录

  • System.getProperty("user.home");
  • 例如输出,C:\Users\hutu

 

雨伞 2. 获取工程目录

  • System.getProperty("user.dir");
  • 例如输出,D:\workspaces\workspace1\myProject

 

雨伞 3. 新建文件,默认位于工程目录

  • new File("xxx.txt").getAbsolutePath();
  • 例如输出,D:\workspaces\workspace1\myProject

 

雨伞 4. 配置文件路径

  • ApplicationContext context = new ClassPathXmlApplicationContext("Config.xml");
    1. .xml, .properties等,默认还是从“工程目录”去找的

1). 倒是也可以用绝对路径,真心不推荐啊,太不优雅了;

2). 或者,将log4j文件置于bin/目录下:

     a). 代码中,PropertyConfigurator.configure("bin/log4j.properties");

     b). 代码中,PropertyConfigurator.configure(ClassLoader.getSystemResource("log4j.properties"));

     c). 注意,必须位于bin直接目录下,不可位于bin更深层的目录当中。可是这究竟是为神马捏?

 

雨伞 5. 查询某类的.class文件所在目录

  • Main.class.getResource(“”);
  • 例如输出,D:\workspaces\workspace1\myProject\bin\com\hutu\log4j\
  • 注意,查询包上级路径,只需将参数改作“/”

 

雨伞 6. 查询thread上下文所在目录

  • Thread.currentThread().getContextClassLoader().getResource("");
  • 例如输出,D:\workspaces\workspace1\myProject\bin\

 

雨伞 7. 查询某类的classloader所在目录

  • Main.class.getClassLoader().getResource("");
  • 例如输出,D:\workspaces\workspace1\myProject\bin\

 

雨伞 8. 查询classloader所在目录

  • ClassLoader.getSystemResource("");
  • 例如输出,D:\workspaces\workspace1\myProject\bin\

雨伞 1. 获取系统根目录

  • System.getProperty("user.home");
  • 例如输出,C:\Users\hutu

 

雨伞 2. 获取工程目录

  • System.getProperty("user.dir");
  • 例如输出,D:\workspaces\workspace1\myProject

 

雨伞 3. 新建文件,默认位于工程目录

  • new File("xxx.txt").getAbsolutePath();
  • 例如输出,D:\workspaces\workspace1\myProject

 

雨伞 4. 配置文件路径

  • ApplicationContext context = new ClassPathXmlApplicationContext("Config.xml");
    1. .xml, .properties等,默认还是从“工程目录”去找的

1). 倒是也可以用绝对路径,真心不推荐啊,太不优雅了;

2). 或者,将log4j文件置于bin/目录下:

     a). 代码中,PropertyConfigurator.configure("bin/log4j.properties");

     b). 代码中,PropertyConfigurator.configure(ClassLoader.getSystemResource("log4j.properties"));

     c). 注意,必须位于bin直接目录下,不可位于bin更深层的目录当中。可是这究竟是为神马捏?

 

雨伞 5. 查询某类的.class文件所在目录

  • Main.class.getResource(“”);
  • 例如输出,D:\workspaces\workspace1\myProject\bin\com\hutu\log4j\
  • 注意,查询包上级路径,只需将参数改作“/”

 

雨伞 6. 查询thread上下文所在目录

  • Thread.currentThread().getContextClassLoader().getResource("");
  • 例如输出,D:\workspaces\workspace1\myProject\bin\

 

雨伞 7. 查询某类的classloader所在目录

  • Main.class.getClassLoader().getResource("");
  • 例如输出,D:\workspaces\workspace1\myProject\bin\

 

雨伞 8. 查询classloader所在目录

  • ClassLoader.getSystemResource("");
  • 例如输出,D:\workspaces\workspace1\myProject\bin\
eclipse项目文件相对路径是相对于项目的根目录来确定的。例如,如果你的项目根目录下有一个名为"images"的文件夹,其中包含一个名为"test.txt"的文件,那么"images/test.txt"就是该文件相对路径。 要创建相对路径文件,你可以按照以下步骤进行操作: 1. 确定需要创建文件相对路径。 2. 检查该路径是否存在。如果不存在,则创建该路径。 3. 使用相对路径创建文件,并获取文件的绝对路径。 以下是一个示例代码,演示了如何在eclipse中创建相对路径文件并获取其绝对路径: ```java import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; public class FileTest { public static void main(String[] args) throws IOException { // 定义文件相对路径 String relativePath = "images/test.txt"; // 创建文件对象 File file = new File(relativePath); // 检查路径是否存在,若不存在则创建路径 if (!file.exists()) { file.mkdirs(); file.createNewFile(); } // 创建文件写入流 FileWriter osw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(osw); // 写入文件内容 String content = "Hello, world!"; bw.write(content); // 关闭文件写入流 bw.close(); // 获取文件的绝对路径 String absolutePath = file.getAbsolutePath(); System.out.println("文件的绝对路径:" + absolutePath); } } ``` 通过以上代码,你可以在eclipse项目中创建相对路径文件,并获取该文件的绝对路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值