IOUtils常用方法的使用

/**
* apache.commons.io.IOUtils常用方法的使用
* 对字节流inputStream,outStream,writer,reader字符流的常用方法的使用
* @author ZYY
*
*/
public class TestCommon {
public static void main(String[] args) throws IOException {
testLineIterator();
}
/**
* CloseQuietly可以关闭inputStream,outputStream,reader,writer流
*
* @throws IOException
*/
public static void testCloseQuietly() throws IOException {
String filename = “D:” + File.separator + “data.txt”;
File f = new File(filename);
InputStream in = new FileInputStream(f);
// 读取内容
byte[] b = new byte[1024];
in.read(b);
System.out.println(new String(b));
OutputStream out = new FileOutputStream(f, true);
String str = “我们是幸运的”;
out.write(str.getBytes());
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
}
/**
* toString的使用
* @throws IOException
*/
public static void testToString() throws IOException {
String filename = “D:” + File.separator + “data.txt”;
File f = new File(filename);
InputStream in = new FileInputStream(f);
System.out.println(IOUtils.toString(in));
IOUtils.closeQuietly(in);
}
/**
* copy的使用
* copy能拷贝Integer.MAX_VALUE的字节数据,即2^31-1。
* 如果是很大的数据,那么可以选择用copyLarge方法,适合拷贝较大的数据流,比如2G以上
* @throws IOException
*/
public static void testCopy() throws IOException {
String filename = “D:” + File.separator + “data.txt”;
String filename2=”D:”+File.separator+”data2.txt”;
File f = new File(filename);
File f2=new File(filename2);
InputStream input1=new FileInputStream(f2);
InputStream in = new FileInputStream(f);
System.out.println(“未复制之前:”+IOUtils.toString(input1));
OutputStream out=new FileOutputStream(f2);
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
InputStream input2=new FileInputStream(f2);
System.out.println(“复制之后:”+IOUtils.toString(input2));
IOUtils.closeQuietly(input1);
IOUtils.closeQuietly(input2);
}
/**
* ToByteArray的使用
* @throws IOException
*/
public static void testToByteArray() throws IOException {
String filename=”D:”+File.separator+”data.txt”;
File f=new File(filename);
InputStream in=new FileInputStream(f);
byte[] byteArray = IOUtils.toByteArray(in);
System.out.println(new String(byteArray));
IOUtils.closeQuietly(in);
}
/**
* 测试write
* @throws IOException
*/
public static void testWrite() throws IOException {
String filename=”D:”+File.separator+”data.txt”;
File f=new File(filename);
OutputStream out=new FileOutputStream(f);
IOUtils.write(“我们是幸运的”, out);
IOUtils.closeQuietly(out);
InputStream in=new FileInputStream(f);
System.out.println(IOUtils.toString(in));
IOUtils.closeQuietly(in);
}
/**
* 测试ToInputStream
* @throws IOException
*/
public static void testToInputStream() throws IOException {

    InputStream inputStream = IOUtils.toInputStream("LHQ是幸运的");
    System.out.println(IOUtils.toString(inputStream));  
    IOUtils.closeQuietly(inputStream);
}
/**
 * 测试ReadLines
 * 分行读取内容
 * @throws IOException
 */
public static void testReadLines() throws IOException {
    String filename="D:"+File.separator+"data.txt";
    File f=new File(filename);
    Reader input=new FileReader(f);
    List readLines = IOUtils.readLines(input);
    for (Object object: readLines) {
        System.out.println(object);
    }
    IOUtils.closeQuietly(input);
}
/**
 * 测试LineIterator
 * LineIterator可以构造器可以接受一个Reader
 * 通常在读取大文件时候将BufferedReader装进去性能会比较好
 * @throws IOException
 */
public static void testLineIterator() throws IOException {
    //于安全考虑这里推荐使用io包的LineIterator,并且其在性能上也优于普通流
    String filename="D:"+File.separator+"data.txt";
    File f=new File(filename);
    Reader reader=new FileReader(f);
    LineIterator lineIterator = IOUtils.lineIterator(reader);
    while (lineIterator.hasNext()) {
        System.out.println(lineIterator.nextLine());
    }
    lineIterator.close();
    IOUtils.closeQuietly(reader);
}

}

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值