Java File path

How to load a properties?

 

Simply add the properties’ folder into the classpath, then load it in this way.

 

private static Properties prop = new Properties();

prop.load(Constants.class.getClassLoader().getResourceAsStream("application.properties"));

 

Class.getResourceAsStream() & ClassLoadergetResourceAsStream()


注意两点:
1,
Class.getResourceAsStream() ,路径应该是以"/"开头的,:
mypackage.Hello.class.getResourceAsStream("/config/config.ini");


2,
如果直接用ClassLoadergetResourceAsStream() 不用以"/"开头.,
mypackage.Hello.class.getResourceAsStream("config/config.ini");

 

 

 

 

All kinds of methods to get path

 

package com.zcjl.test.base;
import java.io.File;
public class Test

{
  public static void main(String[] args) throws Exception

   {
  System.out.println(
  Thread.currentThread().getContextClassLoader().getResource(""));
  System.out.println(Test.class.getClassLoader().getResource(""));
  System.out.println(ClassLoader.getSystemResource(""));
  System.out.println(Test.class.getResource(""));
  System.out.println(Test.class.getResource("/"));
  System.out.println(new File("").getAbsolutePath());
  System.out.println(System.getProperty("user.dir"));
  }
 }
 

  file:/E:/workSpace/javaTest/target/classes/
  file:/E:/workSpace/javaTest/target/classes/
  file:/E:/workSpace/javaTest/target/classes/
  file:/E:/workSpace/javaTest/target/classes/javaAPI/
  file:/E:/workSpace/javaTest/target/classes/
  E:\workSpace\javaTest
  E:\workSpace\javaTest

 

 

. and .. are evil

 

That’s because . always means the current folder, and .. always means the parent folder. This is a dirty evil solution. Though it looks like it works fine now, but you really don’t know when it will break.

 

That’s because you will never know how and where your program will be invoked.

 

For example, your code make be called from an ant build.xml file, from a direct unit test java program, from a batch file. And these invoker entities have different locations. As these calling locations change, the value of . and .. also change.

 

The solution is, use absolute location.

The solution is, use system properties.

 

When you start your application, always pass in a project_root system properties.

 

As long as you have this system properties configged, you can config your log4j.xml like this:

 

      <appender name="quantumlogger" class="org.apache.log4j.DailyRollingFileAppender">

            <param name="File" value="${project_root}/log/Quantum.log"/>

            <param name="Append" value="true"/>

            <param name="Threshold" value="DEBUG"/>

            <!-- Rollover at midnight each day -->

            <param name="DatePattern" value="'.'dd-MM-yyyy"/>

            <layout class="org.apache.log4j.PatternLayout">

                <param name="ConversionPattern" value="%d %-5p [%c] (%t:%x) %m%n"/> <!-- This pattern incurs less performance penalty -->

            </layout>      

      </appender>      

 

As long as your have this system properties configged, you can write your UnitTestBase.java like this:

 

public class UnitTestBase

{

      protected static String project_root = null;

     

      static

      {

            if(System.getProperty("project_root") == null)

            {

                  System.setProperty("project_root", "C:/VIPER-DEV/eqtg/dev/hk/trading/BookingEngine");

                  project_root = System.getProperty("project_root");

                 

                  System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

                  System.out.println("system property -- project_root can't be retrieved!!!!!!!!!!!!!");

                  System.out.println("Will use default value: " + project_root);

                  System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

            }

            else

            {

                  project_root = System.getProperty("project_root");

                 

                  System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");

                  System.out.println("system property -- project_root is successfully retrieved: " + project_root);

                  System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");

            }

      }

}

 

 

How to setup a system properties?

1)    Simply use –Dproject_root=somewhere in your java command, in your bat file or somewhere.

2)    Simply use <sysproperty key="project_root" value="${bookingEngine.root}"/> in your build.xml ant file.

3)    There are different ways of setting up a system property based on the context. And this is a popular requirement, simply google it. You will find the answer.

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值