HOME> 葡萄牙世界杯冠军> 开发中常用的Apache开源的操作文件和IO的包

开发中常用的Apache开源的操作文件和IO的包

2025-05-23 04:01:43

Apache开源的IO包和Lang包

开发中我们常常会使用到Apache这两个包, 所以总结一下一些常用的方法

common.io包

common.io包和是apache旗下开源的一个用来操作文件和IO的常用包。

其封装了很多好用的便捷IO API

主要由以下六部分组成

Utility classes - with static methods to perform common tasks

Input - useful Input Stream and Reader implementations

Output - useful Output Stream and Writer implementations

Filters - various implementations of file filters

包含大量已经实现好的过滤器, 可以拿来即用.

文件过滤器

Comparators - various implementations of java.util.Comparator for files

File Monitor - a component for monitoring file system events

commons.lang包

org.apache.commons也是apache开源的包

java的标准类库并没有提供足够多的方法来操作和核心类库。Apache Commons Lang提供了这些额外的方法

主要提供了以下几类方法

字符串操作方法

基本数值类型操作方法

对象反射

并发

对象创建和序列化

注意: Lang3.0(及后续版本)使用org.apache.commons.lang3包, 先前版本是org.apache.commons.lang

maven导入

commons-io

commons-io

2.6

org.apache.commons

commons-lang3

3.7

常用的类

FileUtils

IOUtils

StringUtils

FileUtils常用的API

readFileToString()

功能: 读取文本内容转换为字符串.不会抛出异常

@Test

public void testFileUtils_readFileToString() {

try {

String encoding = "UTF-8";

String str = FileUtils.readFileToString(new File("2.txt"), encoding);

System.out.println(str);

} catch (IOException e) {

e.printStackTrace();

}

}

writeStringToFile()

功能: 将字符串写入到指定文件中.可能会抛出IOException

@Test

public void testFileUtils_writeStringToFile() {

File file = new File("testFileUtils.txt");

String str = "I love bj\nI love bj\n我爱北京\n";

String encoding = "UTF-8";

try {

FileUtils.writeStringToFile(file, str, encoding);

} catch (IOException e) {

e.printStackTrace();

}

}

forceMkdir()

功能: 创建多级目录, 如果父目录不存在则会字自动创建.因为权限等其他原因创建失败会抛出IOException

@Test

public void testFileUtils_forceMkdir() {

File file = new File("/data/1/2/3");

try {

FileUtils.forceMkdir(file);

} catch (IOException e) {

e.printStackTrace();

}

}

forceDelete()

功能: 用于强制删除文件或者目录.可能抛出IOException

@Test

public void testFileUtils_foreceDelete() {

File dirOrFile = new File("/data");

try {

FileUtils.forceDelete(dirOrFile);

} catch (IOException e) {

e.printStackTrace();

}

}

copyFile()

功能: 拷贝文件

@Test

public void testFileUtils_copyFile() throws IOException {

File src = new File("beauty.jpg");

File dest = new File("beauty2.jpg");

FileUtils.copyFile(src, dest);

}

copyDirectory()

功能:拷贝目录

@Test

public void testFileUtils_copyDirectory() {

File srcDir = new File(".idea");

File destDir = new File("copyIDEAConfig");

try {

FileUtils.copyDirectory(srcDir, destDir, new NameFileFilter("java"), true);

} catch (IOException e) {

e.printStackTrace();

}

}

listFiles()

功能:列出当前目录下的所有文件,能够设置是否递归来控制是否获取多级目录

@Test

public void testFileUtils_listFiles() {

File dir = new File(".");

String[] extensions ={"java"};

boolean recursive = true;

Collection files = FileUtils.listFiles(dir, extensions, recursive);

for (File file :

files) {

System.out.println(file);

}

}

StringUtils常用API

字符串空白符和空判断

isBlank(final CharSequence cs)

功能: 判断字符串是否为空白符, 空字符串、空白符和null都为是空白符

@Test

public void testStringUtils_isBlank() {

System.out.println(StringUtils.isBlank(" ")); // 输出true

System.out.println(StringUtils.isBlank(" \t")); // 输出true

System.out.println(StringUtils.isBlank("")); // 输出true

System.out.println(StringUtils.isBlank(null)); // 输出true

System.out.println(StringUtils.isBlank("a")); // 输出false

}

isEmpty(final CharSequence cs)

功能: 判断字符串是否为空.只有空字符串和null表示字符串为空

@Test

public void testStringUtils_isEmpty() {

System.out.println(StringUtils.isEmpty(" ")); // 输出false

System.out.println(StringUtils.isEmpty(" \t")); // 输出false

System.out.println(StringUtils.isEmpty("")); // 输出true

System.out.println(StringUtils.isEmpty(null)); // 输出true

System.out.println(StringUtils.isEmpty("a")); // 输出false

}

字符串操作

join

功能: 主要功能是将数组转成字符串。并且按照指定分隔符拼接。

支持八大基本数据类型数组(处理boolean)、Iterable类型、Object类型数组

@Test

public void testStringUtils_join() {

String str = StringUtils.join(new int[]{1,2,3,4,5}, '-'); // 输出1-2-3-4-5

System.out.println(str);

System.out.println(StringUtils.join(1,3,4, '+')); // 输出123+

}

split

功能: 将字符串转字符串数组。按照指定分隔符进行分割.

@Test

public void testStringUtils_split() {

String[] strs = StringUtils.split("a-b-c-d", '-');

for (String word:strs

) {

System.out.println(word);

}

}

replace

功能: 字符串替换操作,指定源字符串、要匹配的字符串、要替换成的字符串

@Test

public void testStringUtils_replace() {

String str = StringUtils.replace("a-b-c-d", "-", "+");

System.out.println(str);

}

trimToNull

功能: trim的加强版, 如果trim后的字符串为空字符串,则返回null。否者返回trim后的字符串

@Test

public void testStringUtils_trimToNull() {

String str = StringUtils.trimToNull(" abc my love ");

System.out.println(str);

}

IOUtils常用API

拷贝

copy()

功能:copy方法是重载方法,起主要实现了输入流到输出流的拷贝功能

能拷贝文件字节输入流到文件字节输出流,

能拷贝到字符输出流.

能从文件字符输入流中进行拷贝

同时可以设置字符编码, 以正确的解析

将输入流里的数据拷贝到输出流, 可以用来实现文件拷贝功能

@Test

public void testIOUtils() {

InputStream is = null;

OutputStream os = null;

try {

is = new FileInputStream("2.txt");

os = new FileOutputStream("2copy.txt");

IOUtils.copy(is, os);

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

os.close();

} catch (IOException e) {

e.printStackTrace();

}

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

copyLarge()

copy方法其实是对copyLarge方法的包装。

copyLarge方法支持2GB文件以上的拷贝而copy对于2GB以上的大文件拷贝支持不好。

超过2GB以上的用copy方法能拷贝,但是拷贝完后会返回-1

创建输入流

toInputStream

功能: 根据字符串和编码格式直接创建包含该字符串数据的输入流

public void testIOUtils_getInputStream() {

InputStream is = null;

OutputStream os = null;

try {

is = IOUtils.toInputStream("hello beijing", "UTF-8");

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

当然其还能创建以下两种处理流, 文件字节缓冲流和字符缓冲留toBufferedInputStream、toBufferedReader

从流中读取数据

toString(final InputStream input, final String encoding)

功能: 从输入流中根据编码格式, 读取字符串数据

@Test

public void testIOUtils_toString() {

InputStream is = null;

try {

is = IOUtils.toInputStream("hello beijing", "UTF-8");

String strs = IOUtils.toString(is

,

"UTF-8");

System.out.println(strs);

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

还有toString(final Reader input)等重载方法

List readLines(InputStream input, final String encoding)

功能: 从输入流中按行读取数据,而且这是一次性读取出来.没有BufferedReader那个按行读取实用

@Test

public void testIOUtils_readLines() {

InputStream is = null;

try {

is = new FileInputStream("2.txt");

List lines = IOUtils.readLines(is, "UTF-8");

for (String line :

lines) {

System.out.println(line);

}

}catch (IOException e) {

}finally {

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

最后

还有很多的API就不一一总结。总结出常用的API。

这两个包所提供的功能很强大, 熟悉这些API对于开发效率能有很大的提升。

先锋论文检测软件

英语学习机十大品牌排行榜