黑马程序员——Java基础——IO流(下)

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

IO流(下)

其他相关流

如图


1.FIle文件类

用于操作文件的类,详情请查看API

2.打印流

1)PrintStream (字节)

2)PrintWriter(字符)

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;

class PrintDemo
{
	public static void main(String[] args) throws Exception{
		//输入
		//BufferedInputStream cin = new BufferedInputStream(System.in);
		BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
		//字符输出流
		PrintWriter out = new PrintWriter("a.txt");
		//字节输出流
		//PrintStream out = new PrintStream("a.txt");
		String line = null;
		while((line = cin.readLine())!=null){
			out.println(line);
			out.flush();
		
		}
		out.close();
		cin.close();
	}	
}


3.管道流

import java.io.*;
//建议多线程
//读取
class Read implements Runnable
{
	private PipedInputStream in;
	Read(PipedInputStream in){
		this.in = in;
	}
	public void run(){
		try{
			byte[] buf = new byte[1024];

			int len = in.read(buf);

			String s = new String(buf ,0,len);
			System.out.println(s);
			in.close();
		}catch(Exception e){
		}	
	}
}
//写入
class Write implements Runnable
{
	private PipedOutputStream out;
	Write(PipedOutputStream out){
		this.out = out;
	}
	public void run(){
		try{

			out.write("管道".getBytes());
			out.close();
		}catch(Exception e){
		}	
	}
}

class PipedIODemo
{
	public static void main(String[] args) throws IOException{
		PipedInputStream in = new PipedInputStream();
		PipedOutputStream out = new PipedOutputStream();
		in.connect(out);

		Thread t1 = new Thread(new Read(in));
		Thread t2 = new Thread(new Write(out));
		t1.start();
		t2.start();
	}
}


4.序列流(合并流)

import java.io.*l
import java.util.*;

class SequenceInputStreamDemo
{
	public static void main(String[] args) throws IOException {
	//	File f = new File("e:");
		//建立文件输入集合
		Vector<FileInputStream> v = new Vector<FileInputStream>();
		//输入文件
		v.add("1.txt");
		v.add("2.txt");
		//获取文件
		Enumeration<FileInputStream> en = v.elements();
		//合并流
		SequenceInputStream sis = new SequenceInputStream(en);
		//合并后文件
		FileOutputStream fos = new FileOutputStream("4.txt");

		byte[] buf = new byte[1024];
		int len = 0;
		//合并操作
		while((len = sis.read(buf))!=-1){
			fos.write(buf,0,len);
		}
		fos.close();
		sis.close();

	}
}


5.对象序列化流

import java.io.*;

//准备写入的对象,必须实现Serializable接口,标记接口
class Student implements Serializable
{

	public static final long serialVersionUID = 1L;//如果类发生改变,只要ID不变,默认为一个类

	private String name;
	transient private int age;//transient参数,有的话,就不写入
	Student(String name ,int age){
		this.name = name;
		this.age = age;
	}
	public String toString(){
		return name+"  "+age;
	}
}

class ObjectIODemo
{
	public static void main(String[] args) throws Exception{
		//读取
		//ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Object.txt"));
		//oos.writeObject(new Student("xiaoming",22));
		//oos.close();
		//输入
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Object.txt"));
		Student p = (Student)ois.readObject();
		System.out.println(p);
		ois.close();
	}
}


输入输出重定向

System类中的字段:inout,是标准输入输出,默认的话是输入时键盘,输出是显示器

System.in的类型是InputSteam

System.out的类型是PrintSteam

1.输入重定向

import java.io.FileInputStream;

import java.io.IOException;

public class SystemIn {
	//没处理异常
	public static void main(String[] args) throws IOException {
		//重定向输入,从文件输入
		System.setIn(new FileInputStream("aa.txt"));
		byte buff[] = new byte [1024];
		int len = 0;
		String str = null;
		while((len=System.in.read(buff))!=-1){
			str += new String(buff, 0, len);
		}
		System.out.println(str);
	}
}


2.输出重定向

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class SystemOut {
	//没处理异常
	public static void main(String[] args) throws FileNotFoundException {
		//设置输出
		System.setOut(new PrintStream(new FileOutputStream("out.txt")));
		String str = "测试实验";
		System.out.println(str);
	}
}

三种键盘录入方式
A:main方法的args接收参数
B:System.in通过BufferedReader进行包装
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
C:Scanner
Scanner sc = new Scanner(System.in);

你可能感兴趣的:(黑马程序员——Java基础——IO流(下))