JAVA中的各种IO流操作学习笔记

package com.qxtest.iostudy;

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.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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.RandomAccessFile;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Arrays;

public class IOStudy {
    public static void main(String[] args) {
//        byteIO();
//        charIO();
//        charIOReadLine();
//        convert();
//        byteArrIO("自动化测试");
//        dataIOWrite();
//        dataIORead();
//        objectWrite();
//        objectRead();
        rndIO();
    }
    
    //字节流
    public static void byteIO() {
        long startTime = System.currentTimeMillis();//获取当前时间
        //建立联系
        System.out.println("1、建立联系");
        File src = new File("E:/movie/Visio2010-xdowns.rar");
        File dest = new File("E:/updateDir/testTo.rar");
        
        //选择流
        System.out.println("2、选择流");
        InputStream is = null;
        OutputStream os = null;
        
        try {
            //程序运行时间:1025ms
            is = new BufferedInputStream(new FileInputStream(src));
            os = new BufferedOutputStream(new FileOutputStream(dest));
            //程序运行时间:4386ms
//            is = new FileInputStream(src);
//            os = new FileOutputStream(dest);
            //循环读取 输出
            System.out.println("3、循环读取 输出 强制刷出");
            byte[] flush = new byte[1024];
            int len = 0;
            while(-1!=(len=is.read(flush))){
//                System.out.println(len);
//                System.out.println(Arrays.toString(flush));
//                System.out.println(new String(flush));
                os.write(flush, 0, len);
            }
            //强制刷出
            os.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            //释放资源
            System.out.println("4、释放资源");
            try {
                os.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("ok");
        long endTime = System.currentTimeMillis();
        System.out.println("程序运行时间:"+(endTime-startTime)+"ms");

    }

    //字符流
    public static void charIO(){
        File src = new File("E:/updateDir/test.sql");
        File dest = new File("E:/updateDir/testTo.sql");
        
        Reader is = null;
        Writer os = null;
        
        try {
            is = new BufferedReader(new FileReader(src));
            os = new BufferedWriter(new FileWriter(dest));
            
            char[] flush = new char[2014];
            int len = 0;
            while(-1 != (len=is.read(flush))){
                os.write(flush, 0, len);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                os.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    
    //缓冲流readLine()方法
    public static void charIOReadLine(){
        File src = new File("E:/updateDir/test.sql");
        File dest = new File("E:/updateDir/testTo.sql");
        
        BufferedReader is = null;
        BufferedWriter os = null;
        
        try {
            is = new BufferedReader(new FileReader(src));
            os = new BufferedWriter(new FileWriter(dest));
            
//            char[] flush = new char[2014];
//            int len = 0;
//            while(-1 != (len=is.read(flush))){
//                os.write(flush, 0, len);
//            }
            String line = null;
            while(null != (line=is.readLine())){
                os.write(line);
                os.newLine();
            }
            os.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                os.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }

    //转换流
    public static void convert(){
        File src = new File("E:/updateDir/test.sql");
        File dest = new File("E:/updateDir/testTo.sql");

        BufferedReader br = null;
        BufferedWriter bw = null;
        
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(src), "GBK"));
            bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest), "UTF-8"));
            
            String line = null;
            while(null != (line=br.readLine())){
                bw.write(line);
                bw.newLine();
            }
            bw.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                bw.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
    
    //字节数组流
    public static void byteArrIO(String src){
//        String src = "just a test";
        InputStream is = new BufferedInputStream(new ByteArrayInputStream(src.getBytes()));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] flush = new byte[1024];
        int len = 0;
        try {
            while(-1 != (len = is.read(flush))){
                bos.write(flush, 0, len);
            }
            bos.flush();
            byte[] res = bos.toByteArray();
            System.out.println(new String(res));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                bos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    
    //基础数据流写
    public static void dataIOWrite(){
        double j = 1023.999;
        int i = 10;
        long k = 100L;
        String s = "自动化测试autotest";
        
        File dest = new File("E:/updateDir/testTo.data");
        DataOutputStream dos = null;
        try {
            dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));
            dos.writeDouble(j);
            dos.writeInt(i);
            dos.writeLong(k);
            dos.writeUTF(s);
            dos.flush();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                dos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }

    //基础数据流读
    public static void dataIORead(){
        File src = new File("E:/updateDir/testTo.data");
        
        DataInputStream dis = null;
        
        try {
            dis = new DataInputStream(new BufferedInputStream(new FileInputStream(src)));
            double j = dis.readDouble();
            int i = dis.readInt();
            long k = dis.readLong();
            String s = dis.readUTF();
            System.out.println(j+"-->"+i+"-->>"+k+"-->>"+s);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        
    }

    //对象流写 序列化
    public static void objectWrite(){
        //
        User u = new User(10433, "瞿小渣", "tester");
        int[] arr ={1,2,3,45};
        
        File dest = new File("E:/updateDir/testTO.obj"); 
        //选择流
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dest)));
            oos.writeObject(u);
            oos.writeObject(arr);
            oos.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                oos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    //对象流读 反序列化
    public static void objectRead(){
        File src = new File("E:/updateDir/testTO.obj");
        
        ObjectInputStream ois = null;
        
        try {
            ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(src)));
            Object o = ois.readObject();
            if(o instanceof User){
                User u = (User)o;
                //job没有序列化 为null
                System.out.println(u.getId()+"-->"+u.getName()+"-->"+u.getJob());
            }
            o = ois.readObject();
            int[] arr=(int[])o;
            System.out.println(Arrays.toString(arr));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                ois.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    //随机读取流
    public static void rndIO(){
        File src = new File("E:/updateDir/test.py");
        RandomAccessFile rnd = null;
        try {
            rnd = new RandomAccessFile(src, "r");
            rnd.seek(100);
            byte[] flush = new byte[100];
            int len = 0;
            while(-1 != (len=rnd.read(flush))){
                System.out.println(">>"+new String(flush,0,len));
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
        
}

 

转载于:https://www.cnblogs.com/quxiaozha/p/7280622.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值