1.commons-lang3
commons-lang3里面封装了一些非常常用的操作,比如判断字符串是否为空、日期操作等
2.fastjson
fastjson是阿里的东西,用来处理json格式的字符串非常方便
3.FileUtils
FileUtils是org.apache.commons.io这个包下面的一个工具类。主要封装了一些文件操作相关的逻辑。有了这个,不需要再记住那些繁琐的文件读取存储了。
maven依赖:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
*读取全部内容
String content = FileUtils.readFileToString(new File("tttt.txt"), "gbk");
System.out.println(content);
*按行读取
List<String> lines = FileUtils.readLines(new File("tttt.txt"),"gbk");
for (String line : lines) {
System.out.println(line);
}
*文件复制
FileUtils.copyFile(new File("tttt.txt"),new File("tttt1.txt"))
*写文件,最后的true表示追加内容
FileUtils.writeStringToFile(new File("tttt.txt"),"\n追加的内容","gbk",true)
*文件列举,加过滤
Collection<File> files = FileUtils.listFiles(new File("Desktop"), new IOFileFilter() {
//对文件夹内的文件过滤
public boolean accept(File file) {
String name = file.getName();
return true;
}
//s代表文件名,不知道如何生效
public boolean accept(File file, String s) {
return false;
}
}, new IOFileFilter() {
//对文件夹内的文件夹过滤
public boolean accept(File file) {
return false;
}
public boolean accept(File file, String s) {
return false;
}
});
for (File file : files) {
System.out.println(file.getName());
}