从服务器请求文件流工具类,利用commons-io.jar包中FileUtils和IOUtils工具类操作流及文件(示例代码)...

本文详细介绍了Java中IOUtils和FileUtils的常用方法,包括从InputStream转换为字符串,读写文件,复制文件,下载文件到本地,复制文件到指定目录,以及清理和删除目录等操作。示例代码清晰展示了各种功能的使用方式。
摘要由CSDN通过智能技术生成

1.String IOUtils.toString(InputStream input),传入输入流对象,返回字符串,有多重重载,可按需要传参

用例:

@Test

public void showInfoByIOUtils() throws IOException {

URL url = new URL("https://www.baidu.com");

InputStream in = url.openStream();

System.out.println(in);

try {

System.out.println(IOUtils.toString(in));

} catch (IOException e) {

e.printStackTrace();

}

}

2.利用String FileUtils.readFileToString(File file, String encoding)及FileUtils.writeStringToFile(File file, String data, String encoding)读写文件:

用例:

@Test

public void editFile() throws IOException {

String fileName = "d:/11.txt";

File file = new File(fileName);

String fileContent = "";

try {

fileContent = FileUtils.readFileToString(file, "utf8");

} catch (IOException e) {

e.printStackTrace();

}

fileContent += "Helloworld,我也是";

try {

FileUtils.writeStringToFile(file, fileContent, "utf8");

} catch (IOException e) {

e.printStackTrace();

}

}

3.利用FileUtils.copyFile(File srcFile, File destFile)复制文件:

用例:

@Test

public void copyFile() throws IOException {

try {

File src = new File("d:\\11.txt");

File dest = new File("e://11.txt");

FileUtils.copyFile(src, dest);// 清空目的文件,然后再粘贴内容

} catch (IOException e) {

e.printStackTrace();

}

}

4.利用IOUtils.toByteArray(InputStream input)及FileUtils.writeByteArrayToFile(File file, byte[] data)下载文件到本地:

用例:

@Test

public void download() throws IOException {

try {

InputStream in = new URL("http://www.baidu.com/img/baidu_logo.gif").openStream();

byte[] gif = IOUtils.toByteArray(in);

FileUtils.writeByteArrayToFile(new File("e:/test.gif"), gif);// 清空目的文件,然后再粘贴内容

} catch (IOException e) {

e.printStackTrace();

}

}

5.利用FileUtils.copyFileToDirectory(File srcFile, File destDir)复制文件到指定目录:

用例:

@Test

public void copy2dir() throws IOException {

try {

File srcfile = new File("d:\\11.txt");

File destDir = new File("E:\\photoshop cs6");

FileUtils.copyFileToDirectory(srcfile, destDir);// 复制文件到指定目录

} catch (IOException e) {

e.printStackTrace();

}

}

6.利用FileUtils.copyURLToFile(URL source, File destination)复制url对象到指定文件,相当于网页另存为:

用例:

@Test

public void copyUrl2File() throws Exception {

URL url = new URL("http://www.163.com");

File file = new File("E:\\163.html");

FileUtils.copyURLToFile(url, file);

}

7.利用FileUtils.cleanDirectory(File directory)清空目录,利用FileUtils.deleteDirectory(File directory)删除目录:

用例:

@Test

public void cleanOrDeletedeleteDirectory() throws IOException {

try {

File dir = new File("e:\\test");

FileUtils.cleanDirectory(dir);// 清空目录下的文件

FileUtils.deleteDirectory(dir);// 删除目录和目录下的文件

} catch (IOException e) {

System.out.println(e.getStackTrace());

}

}

package com.hexiang.utils; import java.io.*; /** * FileUtil. Simple file operation class. * * @author BeanSoft * */ public class FileUtil { /** * The buffer. */ protected static byte buf[] = new byte[1024]; /** * Read content from local file. FIXME How to judge UTF-8 and GBK, the * correct code should be: FileReader fr = new FileReader(new * InputStreamReader(fileName, "ENCODING")); Might let the user select the * encoding would be a better idea. While reading UTF-8 files, the content * is bad when saved out. * * @param fileName - * local file name to read * @return * @throws Exception */ public static String readFileAsString(String fileName) throws Exception { String content = new String(readFileBinary(fileName)); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(String fileName, String encoding) throws Exception { String content = new String(readFileBinary(fileName), encoding); return content; } /** * 读取文件并返回为给定字符集的字符串. * @param fileName * @param encoding * @return * @throws Exception */ public static String readFileAsString(InputStream in) throws Exception { String content = new String(readFileBinary(in)); return content; } /** * Read content from local file to binary byte array. * * @param fileName - * local file name to read * @return * @throws Exception */ public static byte[] readFileBinary(String fileName) throws Exception { FileInputStream fin = new FileInputStream(fileName); return readFileBinary(fin); } /** * 从输入读取数据为二进制字节数组. * @param streamIn * @return * @throws IOException */ public static byte[] readFileBinary(InputStream streamIn) throws IOException { BufferedInputStream in = new BufferedInputStream(streamIn); ByteArrayOutputStream out = new ByteArrayOutputStream(10240); int len; while ((len
commons-io-2.2.jar是一个用于Java开发的常用依赖包。它提供了许多实用的工具类和方法,用于简化文件的输入输出操作commons-io-2.2.jar包含了多个类,其一些主要的类和功能包括: 1. FileUtils类:提供了一系列静态方法,用于操作文件,如复制、移动、删除、比较文件等。 2. IOUtils类:提供了一系列静态方法,用于操作输入输出,如读取、写入、关闭等。 3. FilenameUtils类:提供了一系列静态方法,用于操作文件名,如获取名字、扩展名、路径等。 4. IOCase类:提供了一系列枚举常量,用于指定文件名比较的大小写敏感性。 使用commons-io-2.2.jar依赖包,可以在项目轻松地进行文件的处理。例如,可以使用FileUtils类的静态方法复制文件、移动文件或删除文件。使用IOUtils类的静态方法可以方便地读取文件内容、写入文件内容,同时还可以保证在操作完毕后,会被正确地关闭。 此外,commons-io-2.2.jar还提供了许多其他有用的功能,例如操作文件的符号链接、文件滚动、文件比较等。它具有良好的性能和稳定性,并且广泛用于各种Java项目。 总结而言,commons-io-2.2.jar是一个重要的Java依赖包,它提供了简化文件的输入输出操作工具类和方法。通过使用此依赖包,可以方便地进行文件操作,提高开发效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值