IOUtils--IO流应用
来源:互联网 发布:工控软件开发是什么 编辑:程序博客网 时间:2024/06/10 10:54
IO字节流基类:--InputStream:--OutputStream:
IO字符流基类:--Reader:--Writer:
IO转换流:InputStreamReader 字节流通向字符流的桥梁,用于操作字节流的字符流对象。
OutputStreamWriter 字符流通向字节流的桥梁,用于操作字符流的字节流对象。
在使用IO流的时候尽量使用自定义小数组和Buffer组合,速度杠杠的!下面是自己写的一个IO工具类,自定义了异常和对IO流的处理异常(要求jdk为1.7版本以上)
public class IOUtils {
/**
* 用io自带缓存流拷贝数据(非文本文件!!!)
*
* @param address1
* 待拷贝文件的地址
* @param address2
* 拷贝文件后的存放地址
* @throws IOException
*/
public static void copyFiles(String address1, String address2)
throws IOException {
// jdk1.7 提供的异常处理方法 不需要手动关流
try (BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(address1));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(address2));) {
int length;
byte[] arr = new byte[1024 * 8];
while ((length = bis.read(arr)) != -1) {
bos.write(arr, 0, length);
}
}
}
/**
* 用io自带缓存流拷贝数据(纯文本文件!!!)
*
* @param address1
* 待拷贝文件的地址
* @param address2
* 拷贝文件后的存放地址
* @throws IOException
*/
public static void copyTxtFiles(String address1, String address2)
throws IOException {
// jdk1.7 提供的异常处理方法 不需要手动关流
try (BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(address1), "UTF-8"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(address2), "UTF-8"));) {
int length;
char[] arr = new char[1024 * 8];
while ((length = br.read(arr)) != -1) {
bw.write(arr, 0, length);
bw.flush();// 字符流需要刷新 因为中间存在一个转换的过程
}
}
}
/**
* 将字符串转换为文件
*
* @param data
* @param address
* @throws IOException
* @throws CustomException
*/
public static void stringToFiles(String data, String address)
throws IOException, CustomException {
if (address == null || address == "") {
throw new CustomException("文件地址为空,请检查重试!");
}
try (BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(address));) {
byte[] bytes = data.getBytes();
bos.write(bytes);
}
}
/**
* 将(本地)文件转换为字符串
*
* @param address
* @return
* @throws IOException
*/
public static String filesToString(String address) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(address), "UTF-8"));
StringBuffer sb = new StringBuffer();
String msg;
while ((msg = br.readLine()) != null) {
sb.append(msg);
}
br.close();
return sb.toString();
}
/**
* 将文件转换为字符串
*
* @param file
* @return
* @throws IOException
*/
public static String filesToString(File file) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "UTF-8"));
StringBuffer sb = new StringBuffer();
String msg;
while ((msg = br.readLine()) != null) {
sb.append(msg);
}
br.close();
return sb.toString();
}
}
/**
* 自定义异常
*
* @author Simon
*
*/
class CustomException extends Exception {
public CustomException() {
super();
}
public CustomException(String message) {
super(message);
}
}
- IOUtils--IO流应用
- IO流之Commons IO 2.5-IOUtils
- Java IO流学习总结八:Commons IO 2.5-IOUtils
- Commons IO -- IOUtils
- Commons-IO学习之IOUtils
- org.apache.commons.io.IOUtils IOUtils.copy与IOUtils.closeQuietly的用法
- IOUtils
- IOUtils
- IOUtils
- 利用commons-io.jar包中FileUtils和IOUtils工具类操作流及文件
- Commons-IO 的 IOUtils 的几个简单例子
- commons-IO 包解析学习---IOUtils篇(2)
- 学习使用apache comments io包中的IOUtils中的方法
- 工具类commons-io中的IOUtils的使用
- 使用apache comments io包中的IOUtils中的方法
- 学习使用apache comments io包中的IOUtils中的方法
- 学习使用apache comments io包中的IOUtils中的方法
- 使用Apache.commons.io文件工具类FileUtils,IOUtils使用
- 阿里云天气预报接口代码
- Android非mtk平台T9的实现
- STM32F407 串口编程USART1,USART2,USART3,UART4
- 找回Centos7的Root密码
- Spring学习笔记2017.03.22
- IOUtils--IO流应用
- S2013中如何解决error C4996: 'fopen'问题
- SSM之整合Redis
- linux-IO和c语言IO
- Linux下的一个文件查询小工具
- 欢迎使用CSDN-markdown编辑器
- plsql练习
- 深入理解Java:注解(Annotation)自定义注解入门
- 将excel表中的数据写到txt文本文件的Python脚本