day10(IO流汇总)

字节流 (Reader,Writer)

    输入流  FileReader

      

public class Demo {
	public static void main(String[] args) throws IOException {
		FileReader fr=new FileReader("b.txt");
		int len;
		while((len=fr.read())!=-1){
			System.out.print((char)len);
		}
		fr.close();
	}
}

  

    输出流  FileWriter

public class Demo {
	public static void main(String[] args) throws IOException {
		FileReader fr=new FileReader("b.txt");
		//创建输出流对象
		FileWriter fw=new FileWriter("a.txt");
		char[] ch=new char[1024];
		int len;
		while((len=fr.read(ch))!=-1){//读数据
			//写出数据
			fw.write(ch,0,len);
		}
		//关闭流
		fr.close();
		fw.close();
	}

  

   高效流    BufferedReader   BufferedWriter

public class Demo {
	public static void main(String[] args) throws IOException {
		//fileReaderDemo();
		FileReader fr=new FileReader("b.txt");
          //创建高效输入字符流对象 BufferedReader br=new BufferedReader(fr); //创建输出流对象 FileWriter fw=new FileWriter("a.txt"); BufferedWriter bw=new BufferedWriter(fw); char[] ch=new char[1024]; String len; while((len=br.readLine())!=null){//读数据 //写出数据 bw.write(len); bw.newLine(); bw.flush(); } //关闭流 br.close(); bw.close(); } }

字符流  (InputStream,OutputStream)

    输入流  FileInputStream

      

public class Demo {
	public static void main(String[] args) throws IOException {
		FileInputStream fis=new FileInputStream("a.txt");
			int len;
			while ((len=fis.read())!=-1) {
				System.out.print((char)len);
			}
			fis.close();
	}
}

    输出流 FileOutputStream

public class Demo {
	public static void main(String[] args) throws IOException {
		//fileReaderDemo();
		//method01();
		FileInputStream fis=new FileInputStream("b.txt");
		FileOutputStream ios=new FileOutputStream("a.txt");
			int len;
			while ((len=fis.read())!=-1) {
				ios.write(len);
			}
			fis.close();
			ios.close();
	}
}

  

    高效流

public class Demo {
	public static void main(String[] args) throws IOException {
		//fileReaderDemo();
		//method01();
		FileInputStream fis=new FileInputStream("b.txt");
		BufferedInputStream bis=new BufferedInputStream(fis);
		FileOutputStream ios=new FileOutputStream("a.txt");
		BufferedOutputStream bos=new BufferedOutputStream(ios);
			byte[] b=new byte[1024];
			int len;
			while ((len=bis.read(b))!=-1) {
				bos.write(b,0,len);
				bos.flush();
			}
			fis.close();
			ios.close();
	}
}

  

转换流

    输入流      InputStreamReader

    输出流      outputStreamWriter

public class Demo {
	public static void main(String[] args) throws IOException {
		//fileReaderDemo();
		//method01();
		FileInputStream fis=new FileInputStream("b.txt");
		InputStreamReader isr=new InputStreamReader(fis);//转化为字符流  转换输入流
		BufferedReader br=new BufferedReader(isr);//包装为高效流
		FileOutputStream ios=new FileOutputStream("a.txt");//转化为字符流  转换输出流
		OutputStreamWriter osw=new OutputStreamWriter(ios);//包装为高效流
		BufferedWriter bw=new BufferedWriter(osw);
			String len;
			while ((len=br.readLine())!=null) {
				bw.write(len);
				bw.newLine();
				bw.flush();
			}
			bw.close();
			br.close();
	}

  

标准流

    输入流

public class Demo {
	public static void main(String[] args) throws IOException {
		//标准输入流
		InputStream in = System.in;
		InputStreamReader fis=new InputStreamReader(in);
		int len;
		while ((len=fis.read())!=-1) {
			System.out.print((char)len);
		}
		fis.close();
	}
}

    输出流

public class Demo {
	public static void main(String[] args) throws IOException {
		PrintStream out = System.out;
		InputStreamReader fis=new InputStreamReader(new FileInputStream("a.txt"));
		OutputStreamWriter osw=new OutputStreamWriter(out);
		int len;
		while((len=fis.read())!=-1){
			osw.write(len);//输出到控制台
		}
		fis.close();
		osw.close();
	}
}

输出流   (不能写出)

    输入流   PrintWriter(父类为Writer)

		PrintWriter pw=new PrintWriter("c.txt");
		pw.println("hello");
		pw.println("world");//将内容写入c.txt文件中   自带刷新功能
		pw.close();

  

对象流   (序列化)

    输出流(将对象写入到文件中,这个对象必须序列化才能写入到文件中   否则会报错)

    输入流 ()

public class ObjectTest {
	public static void main(String[] args) throws IOException, Exception {
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
				"d.txt"));
		ArrayList<Student> array = new ArrayList<Student>();
		Student s1 = new Student("赵云", 15);
		Student s2 = new Student("张飞", 15);
		array.add(s1);
		array.add(s2);
		oos.writeObject(array);//序列化过程
		oos.close();
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(//反序列化
				"d.txt"));
		ArrayList<Student> arrays = (ArrayList<Student>) ois.readObject();
		for (Student student : arrays) {
			System.out.println(student);
		}
	}
}

  

public class Student implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3919560601713535195L;
// serialVersionUID 用来标识类的唯一性 它是根据类的内容计算出来的 如果写入和读出的类的id不一致 则会报错 String name; int age; String sex; public Student(String name,int age){ this.name=name; this.age=age; } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } }

  

错误异常
 java.io.InvalidClassException
local class incompatible: 类不相容
stream classdesc serialVersionUID = -3919560601713535195, local class serialVersionUID = 5350889806824666248

  

解决方案:
给类进行唯一标识  
让类自动生成  UID

  

原文地址:https://www.cnblogs.com/fjkgrbk/p/IOStream.html