读取resources
放在与java 目录平级的resources 下的资源,可以通过以下方法获取
String path = this.getClass().getClassLoader().getResource(name).getPath();
BufferedReader in = new BufferedReader(new FileReader(path));
String str;
String content = "";
while ((str = in.readLine()) != null) {
content += str;
}
in.close();
使用slf4j 日志
这个比log4j 好, 可以使用占位符
protected
static final Logger sLogger = LoggerFactory.getLogger(MyTestClass.class);
sLogger.info("num:{}", this.testNum);
pom.xml, 以下只是摘录,不能直接使用
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
</dependency>
单元测试
- https://blog.youkuaiyun.com/weixx3/article/details/93767257
Unchecked Cast Warning
Map\List 强制类型转换时, 因为容器内元素类型不一定相匹配,所以会出现 这个warning
List<String> names = (List<String>)testNames();
- 要么函数上加 @SuppressWarnings(“unchecked”)
- 要么逐一遍历源对象里的元素,将其类型转换后,再增加到目地对象