原 OSGL 工具库 - IO 操作的艺术

在前面的 OSGL 工具库之编程艺术系列中我们讲述了

本文中我们将介绍如何使用 OSGL IO 工具类来处理输入输出操作.

依旧是老规矩: 用代码来写博客

首先准备测试变量:

@Ignore
public static class Base extends TestBase {

    protected static String content;
    protected static C.List<String> lines;
    protected static int lineNumber;

    @Before
    public void prepareContent() {
        lineNumber = 5 + N.randInt(10);
        lines = C.newList();
        for (int i = 0; i < lineNumber; ++i) {
            lines.add(S.random());
        }
        content = S.join(lines).by("\n").get();
    }
}

下面的测试代码可以看出如何使用 IO 库进行操作:

public static class FluentIOTest extends Base {
    @Test
    public void testReadStringIntoLines() {
        eq(lines, IO.read(content).toLines());
    }

    @Test
    public void testWriteStringIntoOutputStream() {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IO.write(content).to(baos);
        eq(content, new String(baos.toByteArray()));
    }

    @Test
    public void testWriteByteArrayIntoOutputStream() {
        byte[] ba = {1, 2, 3};
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IO.write(ba).to(baos);
        eq(ba, baos.toByteArray());
    }

    @Test
    public void testWriteByteArrayIntoFile() throws IOException  {
        File file = File.createTempFile("osgl", ".tmp");
        byte[] ba = {1, 2, 3};
        IO.write(ba).to(file);
        byte[] ba0 = IO.read(file).toByteArray();
        eq(ba, ba0);
    }
}

其他 IO 工具演示:

Flushable f;
IO.flush(f);

Closable c;
IO.close(c);

InputStream is;
is = IO.inputStream(); // an empty inputstream
is = IO.inputStream("ABC"); // an input stream of string "ABC"
is = IO.inputStream(new byte[]{1, 2, 3}); 
is = IO.inputStream(new File("/etc/bash.bashrc"));
is = IO.inputStream(MyClass.getResource("/app.properties"));

Reader r;
r = IO.reader(); // an empty reader
r = IO.reader("ABC");
r = IO.reader("ABC".getBytes()):
r = IO.reader(new File("/etc/bash.bashrc"));
r = IO.reader(MyClas.getResource("/app.properties"));

File tmpDir = new File("/tmp");
File tmpFoo = IO.child(tmpDir, "foo.txt"); // /tmp/foo.txt
File tmpDir2 = IO.parent(tmpFoo);
List<File> files = IO.children(tmpDir);

String s0 = IO.checksum(new File("/etc/bash.bashrc"));
String s1 = IO.checksum(IO.inputStream(new File("/etc/bash.bashrc"));

BufferedInputStream is0 = IO.buffered(myOtherInputStream);
BufferedOutputStream os0 = IO.buffered(myOtherOutputStream);
BufferedReader r0 = IO.buffered(myOtherReader);
BufferedWriter w0 = IO.buffered(myOtherWriter);

File tmpZip = IO.zip(f0, f1, ...);
File givenZipFile = ...
IO.zipInto(givenZipFile, f0, f1, ...);

特别提示, OSGL IO 库所有的操作都不会抛出 IOException 异常, 如果有 IO 异常, OSGL IO 库会抛出 UnexpectedIOException 运行时异常.

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏pangguoming

Spring Boot集成JasperReports生成PDF文档

由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲...

1.2K7
来自专栏张善友的专栏

Miguel de Icaza 细说 Mix 07大会上的Silverlight和DLR

Mono之父Miguel de Icaza 详细报道微软Mix 07大会上的Silverlight和DLR ,上面还谈到了Mono and Silverligh...

2717
来自专栏落花落雨不落叶

canvas画简单电路图

62011
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

3145
来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2536
来自专栏菩提树下的杨过

Flash/Flex学习笔记(23):运动学原理

先写一个公用的小球类Ball: package{ import flash.display.Sprite; //小球 类 public class B...

25310
来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

2645
来自专栏芋道源码1024

熔断器 Hystrix 源码解析 —— 断路器 HystrixCircuitBreaker

本文主要基于 Hystrix 1.5.X 版本 1. 概述 2. HystrixCircuitBreaker 3. HystrixCircuitBreaker....

5327
来自专栏张善友的专栏

Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2577
来自专栏大内老A

The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciti...

31810

扫码关注云+社区