java常用IO流集合用法模板

package com.fmy;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.CharArrayReader;
import java.io.CharArrayWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Serializable;
import java.io.Writer;


public class Demo1 {


	static File file1 =new File("D:/info1.txt");
	static File file2 =new File("D:/info2.txt");


	/**
	 * 使用字节流复制文本
	 */
	public static void demo1(){
		InputStream is = null;
		OutputStream os = null;
		try {
			is = new FileInputStream(file1);
			os = new FileOutputStream(file2);
			byte []buf =new byte[1024];
			int len;
			while ((len=is.read(buf))!=-1) {
				System.out.println(new String(buf,0,len));
				os.write(buf, 0, len);
			}


		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				is.close();
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	/**
	 * 使用字符流赋值文本
	 */
	public static void demo2() {
		Writer w=null;
		Reader r=null;
		try {


			w = new FileWriter(file2);
			r = new FileReader(file1);


			char buf[] = new char[512];
			int len;
			while ((len=r.read(buf))!=-1) {
				w.write(buf);
			}


		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				w.close();
				r.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}


	}
	/**
	 * 使用字节流赋值文本 ---使用包装流
	 */
	public static void demo3() {
		InputStream is=null;
		OutputStream os=null;
		BufferedInputStream bis=null;
		BufferedOutputStream bos=null;
		try {
			is = new FileInputStream(file1);
			os = new FileOutputStream(file2);
			bis = new BufferedInputStream(is);
			bos = new BufferedOutputStream(os);


			byte[] buf =new byte[1024];
			int len;
			while ((len=bis.read(buf))!=-1) {
				bos.write(buf,0,len);
			}


		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				bos.close();
				bis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


	/**
	 * 使用字符流赋值文本-使用包装流
	 */
	public static void demo4() {
		Writer w = null;
		Reader r = null;
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			r = new FileReader(file1);
			w = new FileWriter(file2);
			br = new BufferedReader(r);
			bw = new BufferedWriter(w);
			String buf="";
			while ((buf=br.readLine())!=null) {
				bw.write(buf);
				bw.newLine();
			}


		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				bw.close();
				br.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}


	}


	/**
	 * 字节流复制文本 ---运用转化流
	 */
	public static void demo5(){
		BufferedWriter bw = null;
		BufferedReader br = null;
		InputStream is = null;
		OutputStream os = null;
		try {
			is = new FileInputStream(file1);
			os = new FileOutputStream(file2);
			bw = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
			br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
			String buf="";
			while ((buf=br.readLine())!=null) {
				bw.write(buf);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				bw.close();
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}


	}


	/**
	 * 使用printStream 输出一个文本
	 */
	public static void demo6(){


		OutputStream os = null;
		PrintStream  ps = null;
		try {
			os = new FileOutputStream(file2);
			ps = new PrintStream(os);
			ps.println(true);
			ps.println("你好漂亮");
			ps.print('我');
			ps.print('呸');
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			ps.close();
		}


	}


	/**
	 * 使用printWriter输出一个文本
	 */
	public static void demo7(){


		Writer w = null;
		PrintWriter pw = null;


		try {
			w = new FileWriter(file2);
			pw = new PrintWriter(w);
			pw.println(98);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			pw.close();
		}
	}


	/**
	 * 使用 CharArrayReader 和 CharArraysWriter 复制文本
	 */
	public static void demo8() {
		Writer w = null;
		Reader r = null;
		CharArrayReader car = null;
		CharArrayWriter caw = null;
		try {
			w = new FileWriter(file2);
			r = new FileReader(file1);
			char [] buf=new char[512];
			caw = new CharArrayWriter();
			int len;
			while ((len=r.read(buf))!=-1){
				caw.write(buf, 0, len);
			}
			car = new CharArrayReader(caw.toCharArray());


			while ((len=car.read(buf))!=-1) {
				w.write(buf, 0, len);
			}


		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if (car!=null) {
				car.close();
			}
			if (caw!=null) {
				caw.close();
			}
			try {
				if (w!=null) {
					w.close();
				}
				if (r!=null) {
					r.close();
				}
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}






	}


	/**
	 * 使用ByteArrayInputStream和 ByteArrayOutputStream
	 */
	public static void demo9() {


		InputStream is = null;
		OutputStream os = null;
		ByteArrayInputStream bais = null;
		ByteArrayOutputStream baos = null;
		try {
			is = new FileInputStream(file1);
			os = new FileOutputStream(file2);
			baos = new ByteArrayOutputStream();
			byte [] buf = new byte [1024];
			int len;


			while ((len=is.read(buf))!=-1) {
				baos.write(buf,0,len);
			}
			bais =new ByteArrayInputStream(baos.toByteArray());
			while ((len=bais.read(buf))!=-1) {
				os.write(buf, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				bais.close();
				baos.close();
				os.close();
				is.close();
			} catch (Exception e2) {
				e2.printStackTrace();
			}
		}
	}




	/**
	 * DataInputStream DataOutputStream使用
	 */
	public static void demo10(){
		DataInputStream dis = null;
		DataOutputStream dos = null ;
		InputStream is = null;
		OutputStream os = null;


		try {
			is = new FileInputStream(file2);
			os = new FileOutputStream(file2);
			dis = new DataInputStream(is);
			dos = new DataOutputStream(os);


			dos.writeUTF("我喜欢你很久了");
			dos.writeFloat(13);


			dos.close();
			System.out.println(dis.readUTF());
			System.out.println(dis.readFloat());
			dis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}


	}


	/**
	 * 使用ObjectInputSteam和ObjectOutStream
	 */
	public static void demo11() {


		InputStream is = null;
		OutputStream os = null;
		ObjectInputStream ois = null;
		ObjectOutputStream oos = null;


		try {
			is = new FileInputStream(file1);
			os = new FileOutputStream(file1);
			oos = new ObjectOutputStream(os);
			ois = new ObjectInputStream(is);
			
			oos.writeObject(new Studen(13,"嘿嘿",124125,123));
			oos.close();
			
			Studen s = (Studen)ois.readObject();
			System.out.println(s.toString());
			ois.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}


	}


	public static void main(String[] args){
		demo11();
	}


}
class Studen implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 8485022571103676109L;


	private  transient int  age = 1;//序列化时只会保存age的默认值0 如果是String则为null 其他略....
							
	static private   String name="张三"; //序列化时只会保存静态赋值的数值。不管后期对象如何new 序列化存储为张三
	/*这个测试成功,是因为都在同一个机器(而且是同一个进程),
	 * 因为这个jvm已经把name加载进来了,所以获取的是加载好的
	 * name,如果你是传到另一台机器或者你关掉程序
	 * 重写写个程序读入Studen.obj,此时因为别的机器或新
	 * 的进程是重新加载name的,所以name信息就是初始时的信息。
	 * */
	private  int salary=100;//实验表明可以序列化
	private int weight;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
	public int getWeight() {
		return weight;
	}
	public void setWeight(int weight) {
		this.weight = weight;
	}
	@Override
	public String toString() {
		return "Studen [age=" + age + ", name=" + name + ", salary=" + salary
				+ ", weight=" + weight + "]";
	}
	public Studen(int age, String name, int salary, int weight) {
		super();
		this.age = age;
		this.name = name;
		this.salary = salary;
		this.weight = weight;
	}
	


}

转载于:https://www.cnblogs.com/muyuge/p/6152335.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值