装饰流

装饰流

java操作流的方式有很多种,包括节点流和装饰流两大类。

  • 节点流是直接操作流的方法,前面几种都是节点流。
  • 装饰流是为了在操作流的时候实现某种效果,而对节点流进行特定修饰的流(例如提高操作流的速度,操作对象等等)。

直接操作流的都是节点流,装饰流是操作节点流进而操作流

字节缓冲流

作用:提高字节流的速度

void byteRead(String name) {
		//创建源
				File src = new File(name);
				//选择流
				//try...with...sourse 更方便的关闭流
				try (InputStream is = new BufferedInputStream( new FileInputStream(src))){
					//操作
//					1,一个一个读
//					int temp;
//					while((temp = is.read()) != -1) {
//						System.out.print((char)temp);
//					}
					
//					2,一段一段读
					byte[] flush = new byte[1024];
					int len = -1;
					while((len = is.read(flush)) != -1) {
						String temp = new String(flush,0,len);
						System.out.print(temp);
					}
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}

	}
	void byteWrite(String name) {
		//1,创建源
		File file = new File(name);
		//2,选择流
		OutputStream os = null;
		
		try {
			os = new BufferedOutputStream(new FileOutputStream(file,false));
			//3,操作
			String msg = "IO is so easy";
			byte[] bt = msg.getBytes();
			os.write(bt,0,bt.length);
			os.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			//4,关闭
			try {
				if(os != null)
					os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

其中:

try (InputStream is = new BufferedInputStream( new FileInputStream(src)))

是try…with…sourse,在try后面加个括号,括号内声明定义流,去掉try…catch的finally,可以自动关闭流。

字符缓冲流

作用:提高字符流的速度

void charRead(String name) {
		//1,源
		File file = new File(name);
		//2,流
		Reader mr = null;
		try {
			mr = new BufferedReader(new FileReader(file));
			char[] flush = new char[1024];
			int len = -1;
			while((len = mr.read(flush)) != -1) {
				String temp = new String(flush,0,len);
				System.out.print(temp);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(mr != null)
					mr.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	void charWrite(String name) {
		//1,源
		File file  = new File(name);
		//2,流
		Writer mw = null;
		try {
			//3,操作
			mw = new BufferedWriter(new FileWriter(file));
			String msg = "王二狗是天才";
			mw.write(msg.toCharArray());
			mw.flush();
			//mw.append(msg.toCharArray());
			mw.append("王二狗是天才");
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(mw != null)
					mw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

转换流

作用:自动转换字符集(默认转换成工程字符集)

package javaClass;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
/**
 * 以字符流形式操作字节流(纯文本的)
 * 处理字符集
 * TODO
 * @version 1.0
 * @author 王星宇
 */
public class testByteChar {
	public static void main(String[] args) {
//		1,以字符流形式操作字节流(纯文本的)
//		try(BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
//		BufferedWriter write = new BufferedWriter(new OutputStreamWriter(System.out));){
//			//循环获取键盘输入
//			String msg = "";
//			while(!(msg.equals("exit"))) {
//				msg = read.readLine();
//				write.write(msg);
//				write.newLine();
//				write.flush();
//			}
//		} catch (IOException e) {
//			System.out.println("操作异常");
//		}
		
//		2,操作网络流,下载baidu HTML文本
		try(BufferedReader read = 
				new BufferedReader(
						new InputStreamReader(
								new URL("https://www.baidu.com").openStream(),"UTF-8"));){
			String temp = null;
			while((temp = read.readLine()) != null) {
				System.out.print(temp);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}

数据流

作用:在存数据的时候保留数据类型

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(baos));
		
		dos.writeUTF("王二狗是天才");
		dos.writeInt(21);
		dos.writeBoolean(true);
		dos.flush();//刷新
		
		byte[] bs = baos.toByteArray();
		
		DataInputStream dis = new DataInputStream(
								new BufferedInputStream (
									new ByteArrayInputStream(bs)));
		
		String name = dis.readUTF();
		int year = dis.readInt();
		boolean TOF = dis.readBoolean();
		
		System.out.println(name + " " + year + " " + TOF);
		

对象流

作用:序列化对象,从而可以以对象为单位存储和读取

并不是所有的对象都可以序列化,要序列化必须要实现java.io.Serializable(空接口)

package javaClass;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 对象流   序列化
 * TODO 
 * @version 1.0
 * @author 王星宇
 * 1,先序列化,再反序列化
 * 2,先写的后读
 * 3,不是所有的对象都可以序列化,要实现一个空接口
 */
public class testObject {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(baos));
		oos.writeObject(new ss("王二狗",21));
		oos.flush();
		byte[] bbs = baos.toByteArray();
		ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(bbs)));
		Object temp = ois.readObject();
		ss s = null;
		if(temp instanceof ss)
			s = (ss)temp;
		System.out.println(s);
	}
}
//javabean
class ss implements java.io.Serializable{
	private transient String name; //序列化时透明,该数据不需要序列化
	private int year;
	public ss(String name, int year) {
		super();
		this.name = name;
		this.year = year;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	
	@Override
	public String toString() {
		String info = name + ": " + year + "岁";
		return info;
	}
	
}

打印流

PrintWriter,PrintStream两个类作用相同
作用:操作打印流
代码没问题,只不过在我电脑上没法重定向会控制台(两种方法都没用,不知道为什么)

//		PrintWriter 相同,多了一个方法
		PrintStream ps = System.out;
		ps.println("王二狗是天才");
		ps.print(111);
		ps.close();
		PrintStream sysout = System.out;
		
		ps = new PrintStream(new BufferedOutputStream(new FileOutputStream("ll.txt")),true);
		ps.println("王二狗是天才");
		ps.println(111);
//		ps.flush();
//		ps.close();
		
		//重定向输出端
		System.setOut(ps);
		System.out.println("我在这里");
		
		//重定向回控制台
//		System.setOut(new PrintStream(
//				new BufferedOutputStream(
//						new FileOutputStream(
//								FileDescriptor.out)),true));
		System.setOut(sysout);
		System.out.println("I am back");
		System.out.println("?");
		
		ps.close();

序列流

作用:将几个流放在一起,诶个使用,便于文件的合并

没有代码。。

SequeueInputStream

随机流

作用:可以移动光标,从光标出开始读取,读取一定长度

		RandomAccessFile raf = new RandomAccessFile("ll.txt", "r");
		int needlen = 13; //截取长度
		raf.seek(2);	//起始位置
		byte[] temp = new byte[1024];
		int len = -1;
		while((len = raf.read(temp)) != -1) {
			if(needlen >= len) {
				System.out.println(new String(temp,0,len));
				needlen -= len;
			}else {
				System.out.println(new String(temp,0,needlen));
				break;
			}
		}
		raf.close();
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值