Java IO读写

参考网站:http://blog.csdn.net/u012702547/article/details/45200689
Java流的分类,一般可按以下方式分:


1.按方向分,分为输入流,输出流。
2.按类型分,分为字节流和字符流。 
2.1字节流是通过字节来读取数据 
2.2字符流是通过字符来读取数据
3.按操作方式分,分为节点流和过滤流。 
3.1 可以直接创建的流称为节点流,比如输入流,输出流 
3.2 过滤流可以装饰节点流,让流的功能变得更加强大,过滤流采用装饰者模式,对输入流进行包装。比如说BufferedInputStream,BufferedOutputStream,DataInputStream,DataOutputStream都是过滤流。
4.转换流。


使用字节流读取文件内容
 public void test1(){
        File file = new File("F:\\hello.txt");
        FileInputStream fis = null;
        try {
            //创建一个文件输入流
            fis = new FileInputStream(file);
            //创建一个字节数组用来存储读取的信息
            byte[] buf = new byte[1024];
            //len表示读取的长度
            int len = 0;
            //只要len大于-1说明读取到元素,可对元素直接进行操作
            while((len=fis.read(buf))>-1){
                //通过控制台输出程序,需要指明输出的长度
                System.out.write(buf,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if (fis!=null) {
                    //操作完成之后关闭流
                    fis.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }



通过操作字节流来实现简单的文件拷贝
public void test2(){
        long startTime = new Date().getTime();
        File file = new File("F:\\mysql-installer-community-5.6.22.0.msi");
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(file);
            fos = new FileOutputStream("F:\\1.msi");
            byte[] buf = new byte[1024];
            int len = 0;
            while((len=fis.read(buf))!=-1){
                fos.write(buf, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(fis!=null) fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fos!=null) fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


        long endTime = new Date().getTime();
        //查看效率
        System.out.println((endTime-startTime)/1000);
    }

经过BufferedInputStream和BufferedOutputStream包装后的相同文件的拷贝
public void test3(){
        long startTime = new Date().getTime();
        File file = new File("F:\\mysql-installer-community-5.6.22.0.msi");
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            fis = new FileInputStream(file);
            //将fis包装起来
            bis = new BufferedInputStream(fis);
            //将输出流包装
            bos = new BufferedOutputStream(new FileOutputStream("F:\\2.msi"));
            byte[] buf = new byte[1024];
            int len = 0;
            while((len=bis.read(buf))!=-1){
                bos.write(buf, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            //关闭流之后会自动flush
            try {
                if(bis!=null) bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(bos!=null) bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long endTime = new Date().getTime();
        System.out.println((endTime-startTime)/1000);
    }




=====================================================================
把一个对象写入到文件中
public class User implements Serializable{


    private String username;
    private String password;


    //添加了transient属性的字段不会被存储
    private transient int money;






    public int getMoney() {
        return money;
    }
    public void setMoney(int money) {
        this.money = money;
    }
    public User() {
    }
    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}




public void writeObject(){
        User user = new User("zhangsan","123");
        user.setMoney(200);
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("F:\\object.dat"));
            oos.writeObject(user);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(oos!=null) oos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



从文件中读取一个对象
public void readObject(){
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("F:\\object.dat"));
            User u = (User) ois.readObject();
            System.out.println(u.getUsername()+","+u.getPassword()+","+u.getMoney());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }






猜你喜欢

转载自blog.csdn.net/u013764330/article/details/80198113