输入输出流

来源:互联网 发布:电子显微镜原理 知乎 编辑:程序博客网 时间:2024/04/28 13:29

流的分类:

按数据方向分:输入流和输出流

输入流:InputStream/Reader

输出流:OutputStream/Writer

按数据类型分:

字节流:InputStream/OutputStream

字符流:Reader/Writer


流的选择:在处理纯文本文件时,优先考虑使用字符流,除此之外都使用字节流

原因是:硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容,

但字符只是在内存中才会形成的,所以开发中,字节流使用广泛


文件的对拷(字节流)如下:

package com.lovo.io;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class TestIoByte {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFileOutputStream fos = null;//选择流FileInputStream fis = null;try {//操作流fis = new FileInputStream("C:" + File.separator +"222.jpg");//将C:下的文件读入程序fos = new FileOutputStream("G:" + File.separator+"111.jpg");//将程序中文件读出到G:byte[] buffer = new byte[1024];int length = 0;while((length = fis.read(buffer))!= -1){//读文件fos.write(buffer,0,length);//写文件fos.flush();//冲刷,即读进来多少就写出去多少}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(fos!= null){try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(fis!=null){try {fis.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}

文件的传输(字符流)如下:

package com.lovo.io;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;
public class TestIoChar {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubFileWriter fw = null;FileReader fr = null;try {fr = new FileReader("C:"+File.separator+"5-19.txt");fw = new FileWriter("G:"+File.separator+"am.txt");char[] buff = new char[1024];int length = 0;while((length = fr.read(buff))!=-1){fw.write(buff,0,length);fw.flush();}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(fw!=null){try {fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(fr!=null){try {fr.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}}


对象的序列化与反序列化如下:

package com.lovo.io;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.io.Serializable;public class  TestIoObject implements Serializable{private String name;private int age;private String ades;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getAdes() {return ades;}public void setAdes(String ades) {this.ades = ades;}public TestIoObject(String name, int age, String ades) {super();this.name = name;this.age = age;this.ades = ades;}@Overridepublic String toString() {return "姓名:" + name +"\n "+"年龄:" + age+"\n" + "地址:" + ades+ "\n";}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubTestIoObject tj = new TestIoObject("meimei",15,"石林");/*** * 对象序列化的类, 必须实现Serializable接口  即   implements Serializable  */ObjectOutputStream oos = null;//选择流FileOutputStream fo = null;try {fo = new FileOutputStream("man.txt");//操作流oos = new ObjectOutputStream(fo);oos.writeObject(tj);//对象序列化,将对象以二进制流的方式写到文件} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(oos!=null){try {oos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(fo!=null){try {fo.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}/* * 对象的反序列化 */FileInputStream fi = null;ObjectInputStream ois = null;try {fi = new FileInputStream("man.txt");//选择流,从文件中把二进制流转换成对象ois = new ObjectInputStream(fi);tj=(TestIoObject) ois.readObject();//反序列化} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally{if(ois!=null){try {ois.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(fi!=null){try {fi.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}System.out.println(tj.toString());//System.out.println(tj.getName()+"\n"+tj.getAge()+"\n"+tj.getAdes());}}





0 0