我想从中下载一个文件,并且为此URL使用commons-io 。当我下载时,我想根据要下载的文件类型设置超时。基本上,如果无法在指定时间内下载文件,该方法应该返回错误。
我查看了javadocs,发现所有IO操作都是同步的(阻塞IO操作)是否有其他替代库可以提供与commons-io相同的效率和易用性?
我刚刚在我的 pom 文件中看到 Apache commons-collections 有两个不同的组 id:
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
和这个:
<dependency>
<groupId>org.apache.commons.collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
这两个是一样的吗?如果它们相同,那么按照惯例应该使用哪一个?
我一直在我的代码中使用库存JDK集合.Apache Commons Collections框架运行得更快吗?
如果我将Apache Ant添加到项目构建路径中,Apache Commons IO类是否可用?
我需要使用IOUtil类.如果没有请提供正确的JAR文件的下载链接.
在下面的代码中,为什么包含的两行会System.out.println(person); 产生不同的输出?第二行间接调用了Job.toString产生字符串的方法"Manager",但第一行神秘地没有产生Job@28f67ac7。在person.put("a", "b");我看来,这两者之间的界限应该没有任何区别。
代码:
import java.util.*;
import org.apache.commons.lang3.builder.*;
class Job extends HashMap<String, String> {
@Override public String toString() {
return "Manager";
}
}
class Person extends HashMap<String, String> {
Job job;
Person() {
this.job = new Job();
}
@Override public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
class Test {
public static void main(String[] args) {
Person person = new Person();
System.out.println(person);
person.put("a", "b");
System.out.println(person);
}
}
Run Code Online (Sandbox Code Playgroud)
安慰:
Person@2b80d80f[job=Job@28f67ac7,threshold=0,loadFactor=0.75]
Person@2b80d80f[job=Manager,threshold=12,loadFactor=0.75]
Run Code Online (Sandbox Code Playgroud) java tostring apache-commons apache-commons-lang apache-commons-lang3
public static void main(String[] args){
Date date = null;
try {
date = DateUtils.parseDateStrictly("2018-03-11 01:59:00", "yyyy-MM-dd HH:mm:ss");
System.out.println(date.getTime());
date = DateUtils.parseDateStrictly("2018-03-11 02:00:00", "yyyy-MM-dd HH:mm:ss");
System.out.println(date.getTime());
}catch(Exception e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
第一个时间戳被成功解析,但第二个时间戳无法解析.org.apache.commons.lang3.time.DateUtils有错误吗?我正在使用commons-lang3-3.4.jar
1520751540000
java.text.ParseException:无法在org.apache.com上的org.apache.commons.lang3.time.DateUtils.parseDateWithLeniency(DateUtils.java:401)处解析日期:2018-03-11 02:00:00位于com.ecw.vocabulary.mapper.impl.Test.testMetoo的org.apache.commons.lang3.time.DateUtils.parseDateStrictly(DateUtils.java:321)的.lang3.time.DateUtils.parseDateStrictly(DateUtils.java:343) (Test.java:59)at com.ecw.vocabulary.mapper.impl.Test.main(Test.java:40)
我有一个字符串,我想将其解析为布尔值。如果它不包含true或false字符串,它应该返回默认值(true在我的例子中)。
"true" -> true
"false" -> false
"something" -> true
Run Code Online (Sandbox Code Playgroud)
我正在寻找 Java 方法或一些 util(Apache Commons 或 Guava)。
我无法使用Java方法Boolean.parseBoolean,因为它没有带默认值的参数(默认值始终是false):
System.out.println(Boolean.parseBoolean("true")); // true
System.out.println(Boolean.parseBoolean("false")); // false
System.out.println(Boolean.parseBoolean("something")); // false instead of true
Run Code Online (Sandbox Code Playgroud)
与 Apache Commons 相同BooleanUtils.toBoolean:
System.out.println(BooleanUtils.toBoolean("true")); // true
System.out.println(BooleanUtils.toBoolean("false")); // false
System.out.println(BooleanUtils.toBoolean("something")); // false instead of true
Run Code Online (Sandbox Code Playgroud)
我可以写我自己的方法:
private static Boolean toBoolean(String value, boolean defaultValue) {
return Optional.ofNullable(BooleanUtils.toBooleanObject(value))
.orElse(defaultValue);
}
Run Code Online (Sandbox Code Playgroud)
并使用它:
System.out.println(toBoolean("true", true)); // true
System.out.println(toBoolean("false", true)); // false …Run Code Online (Sandbox Code Playgroud)