黑马程序员---Java基础---IO流(三)

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


一.打印流

         1.什么是打印流

                   PrintStream是一个打印流, 在程序中输出数据的时候经常使用这个流, 它既可以输出字节, 也可以输出字符.

                   通常使用PrintStream都是输出指定对象toString()方法的返回值

         2.怎么使用

                   创建PrintStream可以指定一个文件写出, 也可以指定一个字节输出流,可以指定码表和自动flush()

                   print()和println()方法都可以将指定对象的toString()返回值写出

                   println()在写出之后加上换行符号

package it.io2;

import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;

public class PrintStreamDemo {

	public static void main(String[] args) throws IOException {
//		demo1();
//		demo2();
//		demo3();
//		demo4();
//		demo5();
	}

	//创建PrintStream可以指定一个文件写出, 也可以指定一个字节输出流, 可以指定码表和自动flush()
	public static void demo5() throws FileNotFoundException,
			UnsupportedEncodingException {
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("file.txt"));
		PrintStream ps = new PrintStream(bos, true, "UTF-8");//每次输出之后自动调用flush()
		ps.println("张三");
		ps.println("李四");
		ps.println("王五");
		ps.close();
	}

	//PrintStream的print和println方法可以打印实参对象的toString()返回值
	public static void demo4() throws FileNotFoundException {
		PrintStream ps = new PrintStream("file.txt");
		ps.println(97);		// 原理: 97 -> new Integer(97) -> toString() -> "97"
		ps.println(98);
		ps.println(99);
		ps.close();
	}

	//PrintStream的print和println方法写出一个对象的时候,会将对象的toString()返回值写出
	public static void demo3() throws FileNotFoundException, UnsupportedEncodingException {
		Person p1 = new Person("张三", 19);
		Person p2 = new Person("李四", 23);
		PrintStream ps = new PrintStream("file.txt", "UTF-8");//指定码表写出
		ps.println(p1);
		ps.println(p2);
		ps.close();
	}

	//PrintStream的print和println方法可以写出字符
	public static void demo2() throws FileNotFoundException {
		PrintStream ps = new PrintStream("file.txt");
		ps.println("张三");
		ps.println("李四");
		ps.println("王五");
		ps.close();
	}

	//PrintStream是继承与OutputStream的,可以当作一个普通的字节输出流使用
	public static void demo1() throws FileNotFoundException, IOException {
		FileInputStream fis = new FileInputStream("fiel.txt");
		PrintStream ps = new PrintStream("file1.txt");
		byte[] buffer = new byte[8192];
		int len;
		while ((len = fis.read(buffer)) != -1)
			ps.write(buffer, 0, len);
		fis.close();
		ps.close();
	}

}
                   

二.标准输入输出流

         1.什么是标准输入输出流

                   System.in是一个InputStream,它是标准输入流, 默认可以从键盘输入读取字节.

                   System.out是一个PrintStream,它是标准输出流, 默认可以向屏幕输出数据.

         2.改变标准输入输出流

                   使用System.setIn(InputStream)可以将标准输入流指向其他数据源

                   使用System.setOut(PrintStream)可以将标准输出流指向其他的数据目的地

package it.io2;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class StandardStreamDemo {

	public static void main(String[] args) throws IOException {
//		demo1();
//		demo2();
	}

	// 使用System.setIn(InputStream)可以将标准输入流指向其他数据源
	public static void demo2() throws FileNotFoundException, IOException {
		System.setIn(new FileInputStream("file.txt"));
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		System.out.println(br.readLine());
	}

	// 使用System.setOut(PrintStream)可以将标准输出流指向其他的数据目的地
	public static void demo1() throws FileNotFoundException {
		System.setOut(new PrintStream("file.txt"));	
		System.out.println("Hello Wolrd!");
	}

}
                   

三.字节数组输出流

         1.什么是字节数组输出流

                   ByteArrayOutputStream是一个可以向内存中写出字节的一个输出流, 通常可以作为一个缓冲区使用

         2.怎么使用

                   创建对象之后, 可以向ByteArrayOutputStream写入字节数据, 这些数据都会在内存中存储.

                   可以调用toByteArray()方法将写入的数据一次性获取出来

                   如果想清空数据, 可以调用reset()方法

                   获取长度可以调用size()方法

         3.应用场景

                   读取一个文件中的文本数据,转为字符串.

                   如果使用普通的字符输入流读取, 那么会每读一两个字节转为一个字符, 这样效率较低.

                   可以使用FileInputStream将文件数据读取, 写入到ByteArrayOutputStream中, 将文件中所有内容都写入.

                   然后使用ByteArrayOutputStream的toByteArray()方法一次性获取数据转为字符.

package it.io2;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

public class ByteArrayOutputStreamDemo {

	public static void main(String[] args) throws IOException {
//		demo1();
//		demo2();
//		demo3();
//		demo4();
	}

	//用ByteArrayOutputStream读取字符数据
	public static void demo4() throws FileNotFoundException, IOException {
		FileInputStream fis = new FileInputStream("file.txt");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buffer = new byte[8192];
		int len;
		while ((len = fis.read()) != -1)
			baos.write(buffer, 0, len);						//将文件中所有数据写入到内存中
		fis.close();
		baos.close();
		
		String content = new String(baos.toByteArray());	//吧内存中的数据取出,一次性转为字符
		System.out.println(content);
	}

	//读取字符
	public static void demo3() throws FileNotFoundException, IOException {
		FileReader fr = new FileReader("file.txt");
		int i;
		while ((i = fr.read()) != -1)
			System.out.println((char)i);
		fr.close();
	}

	//用ByteArrayOutputStream写入字节数据
	public static void demo2() throws FileNotFoundException, IOException {
		FileInputStream fis = new FileInputStream("file.txt");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();	
		byte[] buffer = new byte[8192];
		int len;
		while ((len = fis.read(buffer)) != -1)
			baos.write(buffer, 0, len);			//将文件中所有数据写入到内存中
		fis.close();
		baos.close();
		
		byte[] data = baos.toByteArray();		//获取内存中的数据
		
		FileOutputStream fos = new FileOutputStream("file1.txt");
		fos.write(data);						//一次性写出到文件
		fos.close();
	}

	//字节流拷贝
	public static void demo1() throws FileNotFoundException, IOException {
		FileInputStream fis = new FileInputStream("file.txt");
		FileOutputStream fos = new FileOutputStream("file1.txt");
		
		byte[] buffer = new byte[8192];
		int len;
		while ((len = fis.read(buffer)) != -1)
			fos.write(buffer, 0, len);
		fis.close();
		fos.close();
	}

}
                   

四.数据输入输出流

         1.什么是数据输入输出流

                   DataInputStream,DataOutputStream, 可以按照基本数据类型大小读写数据

         2.怎么使用

                   使用DataOutputStream可以按照基本数据类型大小写出, 例如: writeInt()方法可以写出一个int值, 占4个字节. writeLong()可以写出long值, 占8个字节

                   使用DataInputStream可以读取DataOutputStream写出的数据, 例如: readInt()可以读取4个字节,readLong()可以读取8个字节

package it.io2;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class DataStreamDemo {

	public static void main(String[] args) throws IOException {
		//记录程序使用次数,从文件读取使用次数,加1在写出
//		demo1();	//只能写0到255
//		demo2();	//每次需要转换
//		demo3();	//可以按照基本数据类型大小读写数据
	}

	public static void demo3() throws FileNotFoundException, IOException {
		DataInputStream dis = new DataInputStream(new FileInputStream("file.txt"));
		int x = dis.readInt();
		System.out.println(x++);
		dis.close();
		
		DataOutputStream dos = new DataOutputStream(new FileOutputStream("file.txt"));
		dos.writeInt(x);;
		dos.close();
	}

	public static void demo2() throws FileNotFoundException, IOException {
		BufferedReader br = new BufferedReader(new FileReader("file.txt"));
		int x = Integer.parseInt(br.readLine());
		System.out.println(x++);
		br.close();
		
		BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"));
		bw.write(x + "");
		bw.close();
	}

	public static void demo1() throws FileNotFoundException, IOException {
		FileInputStream fis = new FileInputStream("file.txt");
		int x = fis.read();
		System.out.println(x++);
		fis.close();
		
		FileOutputStream fos = new FileOutputStream("file.txt");
		fos.write(x);
		fos.close();
	}

}
         

五.对象输入输出流

         1.什么是对象输入输出流

                   ObjectInputStream,ObjectOutputStream, 可以读写对象

         2.怎么使用

                   使用ObjectOutputStream.writeObject()方法可以写出一个实现了Serializable接口的对象

                   使用ObjectInputStream.readObject()方法可以读取ObjectOutputStream写出的对象

         3.读写多个对象

                   如果需要写出多个对象, 通常会将多个对象存入集合,然后将集合写出

                   读取的时候读到集合, 遍历集合即可

package it.io2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;

public class ObjectStreamDemo {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
//		demo1();	//读对象
//		demo2();	//存对象
	}

	public static void demo1() throws IOException, FileNotFoundException,
			ClassNotFoundException {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("file.txt"));
		ArrayList<Person> al = (ArrayList<Person>) ois.readObject();
		ois.close();
		
		for (Person p : al) 
			System.out.println(p);
	}

	public static void demo2() throws IOException, FileNotFoundException {
		Person p1 = new Person("张三", 90);
		Person p2 = new Person("李四", 80);
		Person p3 = new Person("王五", 95);
		
		ArrayList<Person> al = new ArrayList<Person>();
		Collections.addAll(al, p1, p2, p3);
		
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("file.txt"));
		oos.writeObject(al);
		oos.close();
	}

}
         

六.序列流

         1.什么是序列流

                   SequenceInputStream可以用来把多个输入流合并成一个

         2.怎么使用

                   使用构造函数可以接收2个InputStream,或者一个包含多个InputStream的Enumeration

                   创建SequenceInputStream之后, 读取的时候内部会从第一个流逐个读取, 直到所有的流都读完才读到末尾

package it.io2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Vector;

public class SequenceInputStreamDemo {

	public static void main(String[] args) throws IOException {
//		demo1();
//		demo2();
	}

	public static void demo2() throws FileNotFoundException, IOException {
		FileInputStream fis1 = new FileInputStream("a.txt");
		FileInputStream fis2 = new FileInputStream("b.txt");
		FileInputStream fis3 = new FileInputStream("c.txt");
		
		ArrayList<InputStream> al = new ArrayList<InputStream>();	//为了得到Enumeration创建ArrayList
		Collections.addAll(al, fis1, fis2, fis3);					//装入输入流
		Enumeration<InputStream> e = Collections.enumeration(al);	//获取Enumeration
		
		SequenceInputStream sis = new SequenceInputStream(e);		//合并流
		FileOutputStream fos = new FileOutputStream("file.txt");
		int i;
		while ((i = sis.read()) != -1)	//使用SequenceInputStream读取时,先从第一个流开始读,读完读第二个,直到所有的都读完返回-1
			fos.write(i);
		sis.close();
		fos.close();
	}

	public static void demo1() throws FileNotFoundException, IOException {
		FileInputStream fis1 = new FileInputStream("a.txt");
		FileInputStream fis2 = new FileInputStream("b.txt");
		
		SequenceInputStream sis = new SequenceInputStream(fis1, fis2);	//创建序列流,合并两个输入流
		FileOutputStream fos = new FileOutputStream("file.txt");
		
		int i;
		while ((i = sis.read()) != -1)	//使用SequenceInputStream读取时,先从第一个流开始读,读完读第二个,直到所有的都读完返回-1
			fos.write(i);
		sis.close();
		fos.close();
	}

}
                   

七.IO总结

         1.字节流拷贝文件

                   FileInputStream和FileOutputStream,自定义数组

                   BufferedInputStream和BufferedOutputStream,内置缓冲区

         2.字符流读写文本文件

                   InputStreamReader,FileReader, BufferedReader读取字符数据

                   OutputStreamWriter,FileWriter, BufferedWriter写出字符数据

         3.File类

                   拷贝文件夹


------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值