PrintStream PrintWriter Sequence 文件分割 管道流


 

 


PrintWriter对象 

PrintWriter(File file)
PrintWriter(OutputStream out)
PrintWriter(String fileName)
PrintWriter(Writer out)

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

PrintWriter ps = new PrintWriter (System.out);
PrintWriter ps = new PrintWriter ("D:\\aa.txt");
PrintWriter ps = new PrintWriter (new File("D:\\aa.txt"));P
PrintWriter ps = new PrintWriter (new BufferedWriter(new FileWriter("D:\\aa.txt")),true);第二个参数代表是否即时刷数据到硬盘  
PrintStream ps = new PrintStream(new BufferedInputStream(new FileInputStream("D:\\aa.txt")),true); 第二个参数代表是否即时刷数据到硬盘 


 

PrintStream对象

PrintStream(File file)

PrintStream(OutputStream out)PrintStream(String fileName)

使用方法和printWriter差不多  只是不能传递字符流对象进去 如这个方法PrintWriter(Writer out)

 

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintStream ps = new PrintStream(System.out);
		// PrintStream ps = new PrintStream("D:\\aa.txt");
		// PrintStream ps = new PrintStream(new File("D:\\aa.txt"));
		// PrintStream ps = new PrintStream(new BufferedWriter(new
		// FileWriter("D:\\aa.txt")),true); 第二个参数代表是否即时刷数据到硬盘PrintStream ps =
		// new PrintStream(new BufferedInputStream(new
		// FileInputStream("D:\\aa.txt")),true); 第二个参数代表是否即时刷数据到硬盘



 

 

SequenceInputStream对象

主要用法是将多个流组合成一个流对象,读完第一个流到-1的时候并不结束,要读到第最后一个流的-1的时候才结束

示例:

try {
			Vector<FileInputStream> v = new Vector<FileInputStream>();
			v.add(new FileInputStream(new File("D:\\a.txt")));
			v.add(new FileInputStream(new File("D:\\b.txt")));

			ArrayList<FileInputStream> arrayList = new ArrayList<FileInputStream>();
			arrayList.add(new FileInputStream(new File("D:\\a.txt")));
			arrayList.add(new FileInputStream(new File("D:\\b.txt")));
			final Iterator<FileInputStream> iterator = arrayList.iterator();
			Enumeration<FileInputStream> e = new Enumeration<FileInputStream>() {
				public FileInputStream nextElement() {
					return iterator.next();
				}

				public boolean hasMoreElements() {
					return iterator.hasNext();
				}
			};

			Enumeration e = v.elements();
			SequenceInputStream sis = new SequenceInputStream(e);
			byte[] bytes = new byte[1024];
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(new File("D:\\aa.txt")));
			while (sis.read(bytes) != -1) {
				bos.write(bytes);
			}
			sis.close();
			bos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}


 

 

 

文件分割:

	FileInputStream fis = new FileInputStream("D:\\aa.bmp");
		byte[] b = new byte[1024 * 1024];
		FileOutputStream fos = null;
		int len = 0;
		int count = 0;
		while ((len = fis.read(b)) != -1) {
			fos = new FileOutputStream(new File("D:\\" + count + ".part"));
			fos.write(b, 0, len);
			fos.close();
		}


 

ObjectIputStream  ObjectOutputStream

主要功能是对对象进行序列化保存和读取

注意:

   // 使用对象流,对象必须实现Serializable接口,实现地象序列化
   // 静态的字段不能被序列化
   // 要想使用一个字段不能被序列化 使用transient

示例:

try {
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("D:\\aa.txt")));

			Userinfo u = null;

			ObjectOutputStream oos = new ObjectOutputStream(
					new FileOutputStream(new File("D:\\userinfo.object")));
			while ((u = (Userinfo) ois.readObject()) != null) {
				oos.writeObject(u);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


 

 

PipedInputStream  pipedOutputStream管道流:

使用感觉:

管道流主要用于两个线程间的通信,采用多线程writer--read的方法,当线程开启后read处于阻塞读取等待状态,当另外一个线程的writer执行后,read即将管道中的数据读出

官方介绍:

送输入流应该连接到传送输出流;传送输入流会提供要写入传送输出流的所有数据字节。通常,数据由某个线程从PipedInputStream 对象读取,并由其他线程将其写入到相应的PipedOutputStream。不建议对这两个对象尝试使用单个线程,因为这样可能会死锁该线程。传送输入流包含一个缓冲区,可在缓冲区限定的范围内将读操作和写操作分离开。

 

示例:

public static void pipedStream() {
		try {
			PipedInputStream pis = new PipedInputStream();
			PipedOutputStream pos = new PipedOutputStream();
			pis.connect(pos);
			Read r = new Read(pis);
			r.start();
			Write w = new Write(pos);
			w.start();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

class Read extends Thread {
	private PipedInputStream pis;

	public Read(PipedInputStream pis) {
		super();
		this.pis = pis;
	}

	@Override
	public void run() {
		super.run();
		try {
			byte[] b = new byte[1024];
			int len = pis.read(b);
			System.out.println(new String(b, 0, len));

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

}

class Write extends Thread {
	private PipedOutputStream pos;

	public Write(PipedOutputStream pos) {
		super();
		this.pos = pos;
	}

	@Override
	public void run() {
		super.run();
		try {
			pos.write("我可是管道输入流,我现在要往管道里面写东西了".getBytes());
			pos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

} 


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值