《Thinking in java》-学习笔记(10)

本文是《Thinking in java》学习笔记的第10章,主要探讨了Java的IO系统,包括输入输出的层次结构、装饰器设计模式、RandomAccessFile的特性、目录列表器的实现、标准输入输出的重定向、压缩技术如GZIP和Zip,以及对象序列化和控制序列化的细节。章节内容丰富,揭示了Java IO的灵活性和强大功能。
摘要由CSDN通过智能技术生成

第10章 Java IO 系统

1.输入和输出

输入有关的所有类都从InputStream继承,与输出有关的所有类都从OutputStream继承。

2.增添属性和有用的接口

利用层次化对象动态和透明地添加单个对象的能力的做法叫做“装饰器”方案。

3.本身的缺陷:RandomAccessFile

RandomAccessFile不属于InputStream或者OutputStream分层结构的一部分。只能针对

文件才能操作,不能针对数据流操作。可以在一个文件里向前或向后移动。

4.目录列表器

public interface FilenameFilter {
boolean accept(文件目录, 字串名);
}

通过实现这个接口,可以对文件名进行过滤。下面用的是匿名内部类来实现这个接口。

package exam;
import java.io.*;
public class DirList3 {
	public static void main(final String[] args){
		try{
			File path=new File(".");
			String[] list;
			if(args.length==-0)
				list=path.list();
			else
				list=path.list(
						new FilenameFilter(){
							public boolean accept(File dir,String n){
								String f=new File(n).getName();
								return f.indexOf(args[0])!=-1;
							}
						}
			    );
			for(int i=0;i<list.length;i++)
				System.out.println(list[i]);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}

5.IO流的典型应用

package c10;
import java.io.*;
import oypj.tools.*;
public class IOStreamDemo {
	public static void main(String[] args){
		try{
			//1.Buffered input file
			DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream(args[0])));
			String s,s2=new String();
			while((s=in.readLine())!=null)
				s2+=s+"\n";
			in.close();
			//2.Input from memory
			StringBufferInputStream in2=new StringBufferInputStream(s2);
			int c;
			while((c=in2.read())!=-1)
				System.out.print((char)c);
			//3.Formatted memory input
			try{
				DataInputStream in3=new DataInputStream(new StringBufferInputStream(s2));
				while(true)
					System.out.print((char)in3.readByte());
			}catch(EOFException e){
				P.rintln("End of stream encountered");
			}
			//4.Line numbering & file output
			try{
				LineNumberInputStream li=new LineNumberInputStream(new StringBufferInputStream(s2));
				DataInputStream in4=new DataInputStream(li);
				PrintStream out1=new PrintStream(new BufferedOutputStream(new FileOutputStream("IOEemo.out")));
				while((s=
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值