- 浏览: 126903 次
- 性别:
- 来自: Singapore
文章分类
- 全部博客 (112)
- Tiger Thread (18)
- Perforce (6)
- Spring (5)
- maven (3)
- log4j (3)
- Quartz Scheduler (4)
- unix and linux (12)
- hibernate (3)
- Enum (1)
- Futures and Options (1)
- Market Making (2)
- Java Basic (11)
- Tibco EMS (3)
- F I X message (5)
- equity derivative (2)
- Sybase (3)
- XML (1)
- JUnit (2)
- J A X B 2.0 (1)
- N I O (1)
- windows batch file (1)
- Cruise Control (1)
- util Class (5)
- ant (1)
- JMS (1)
- profiling (0)
- Sql Server (6)
- GXT (2)
- eclipse (1)
- Generics (1)
- Tibco RV (3)
- Autosys (0)
- Message (1)
最新评论
-
houzhe11:
<property name="proxyTa ...
AOP usage -- BeanNameAutoProxyCreator usage
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() & ClassLoader的getResourceAsStream()
注意两点:
1,用Class.getResourceAsStream() 时,路径应该是以"/"开头的,如:
mypackage.Hello.class.getResourceAsStream("/config/config.ini");
2,如果直接用ClassLoader的getResourceAsStream() 不用以"/"开头.如,
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.
发表评论
-
equals(), hashCode() and toString()
2009-11-18 21:49 863... -
java start up arg samples
2009-08-24 16:29 811Unix env setup jflags="-m ... -
How to judge whether this is a 32 bit jvm or 64 bit jvm?
2009-08-24 13:33 1217[Solution 1] java –versi ... -
jconsole easy usage
2009-08-22 12:31 758client side java_home/bin/jcon ... -
eclipse remote debug
2009-08-22 10:41 733The most important thing is: ... -
PATH and java.library.path CLASSPATH and java.class.path
2009-07-06 17:17 1808PATH is the environment variabl ... -
RMI quick start
2009-06-19 17:45 568Q) How to start RMI Registry Se ... -
Random usage
2009-04-14 17:23 1001public class RandomTest { ... -
Overwrite readonly File
2009-04-03 11:18 816Readonly file can't be changed, ... -
No Title
2009-03-04 18:07 689About data structure Hashta ...
相关推荐
File file = new File("path_to_your_file.txt"); // 使用FileReader创建字符输入流 FileReader fr = new FileReader(file); // 增加缓冲区,提高读取效率 BufferedReader br = new BufferedReader(fr); //...
Java中的`File`类是Java I/O流体系中不可或缺的一部分,它是用来操作文件和目录的基础类。`File`对象代表了文件和目录路径名的抽象表示。在这个详细的讲解中,我们将深入探讨`File`类的各种功能,包括创建、读取、...
首先,通过`new File(path)`构造函数创建一个`File`对象,其中`path`是文件的路径。然后,调用`exists()`方法检查文件是否存在,如果不存在,再调用`createNewFile()`创建文件。注意,`separator`常量用于获取系统...
### Java File 类的方法详解 #### 一、简介 在Java编程语言中,`java.io.File`类是一个非常重要的类,它提供了对文件和目录路径名的抽象表示,并且支持一些基本的操作,例如创建、删除文件或目录等。本文将详细...
Java中的`File`类是Java I/O流体系中非常基础且重要的部分,它主要用于操作文件和目录。在Java中,如果你需要对文件进行创建、删除、重命名、获取属性等操作,`File`类就是你的首选。下面我们将深入探讨`File`类的...
在本项目中,"JavaFile文件操作"着重介绍了如何使用`File`类进行文件的基本操作,包括创建文件、复制文件或文件夹、删除文件或文件夹以及剪切文件或文件夹。以下是对这些功能的详细说明: 1. **创建文件** 使用`...
### Java中的File类详解 #### 一、概述 在Java编程语言中,`java.io.File`类是一个非常重要的基础类,用于封装一个抽象路径名,该路径名可以表示文件或目录。通过`File`类,我们可以执行一系列与文件和目录相关的...
在Android开发中,Java编程时常会遇到`java.lang.IllegalArgumentException`异常,这通常表示传入的参数不符合方法的要求。在本例中,错误信息“contains a path separator”提示我们问题可能与路径分隔符有关,这...
File file = new File("path/to/your/file.txt"); ``` 然后,我们可以使用`BufferedReader`类来读取文件内容。`BufferedReader`提供了以行为基础的文本阅读,提高了读取效率。下面是如何使用它的示例: ```java ...
1. `File(String path)`:根据给定的路径字符串创建File对象。 2. `File(String parent, String child)`:根据父路径和子路径创建File对象。 **二、文件的创建与删除** - `boolean createNewFile()`:创建一个新的...
JAVA提供了`Path`类和`Paths`工具类,帮助开发者处理文件路径相关的问题。 8. **面向对象编程**: JAVA是一种面向对象的语言,FileEditor可能通过类和对象来组织代码。比如,可能有一个`FileEditor`类,其中包含了...
File file = new File("path/to/your/file"); boolean isHidden = file.isHidden(); ``` 2. **判断文件是否只读**:使用`File.canWrite()`方法。这个方法返回的是文件是否可以被写入,而不是文件是否为只读状态...
String filePath = "path/to/your/file"; File file = new File(filePath); try (InputStream fis = new FileInputStream(file)) { // 设置响应头 setHeaders(response, file.getName()); // 写入输出流 ...
- `File(String path)`:通过指定的路径字符串创建一个`File`对象。 - `File(String parent, String child)`:用父路径和子路径创建一个`File`对象。 2. **创建和删除文件/目录**: - `createNewFile()`:创建一...
java.io clojure.java.io 的 JK7 java.nio.file.Path 兼容性依赖信息该库托管在 Releases 上。 依赖: [me.moocar/java.io " 0.1.0 " ]用法是 JDK7 中引入的文件路径的抽象。 这个库提供了和 Paths 之间的兼容性。 ...
解决 exec: "gcc": executable file not found in %PATH%,被墙了,搭梯下的,解压后直接配置 %path% 路径即可,亲测可用。
### 如何用Java程序设置系统Path环境变量 随着Java技术的发展与普及,越来越多的开发者开始在Windows平台上利用Java进行桌面应用程序开发。在此过程中,经常会出现需要通过JNI(Java Native Interface,Java本地...
System.out.println("Absolute Path: " + file.getAbsolutePath()); System.out.println("Name: " + file.getName()); System.out.println("Can Write: " + file.canWrite()); System.out.println("Can Read: " + ...