JavaIO流问题总结

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.io.*;
import java.util.Date;

public class InTest {
    public static void main(String[] args)throws IOException {
        //init();
       // testDataIO();

      //  testPrintIO();

      //  testPrintStream();
        testObjectIO();
    }

    private static void init(){
       InputStreamReader isr =  new InputStreamReader(System.in);
       BufferedReader br = new BufferedReader(isr);
       String cmd = null;

       try{
           do{
               System.out.print("请输入命令:");
               cmd = br.readLine();
                cmdReceive(cmd);//接收命令并分发
               //do something
           }while(!cmd.equalsIgnoreCase("exit"));

           br.close();//关闭输入流
       }catch(IOException e){
           e.printStackTrace();
           System.exit(-1);
       }

    }

    private static void cmdReceive(String cmd){
        System.out.println(cmd);
    }

    private static void testDataIO(){
        String path = "data" + File.separator + "int.dat";
        File file = new File(path);
        DataOutputStream dos = null;
        DataInputStream dis = null;


        try{
            dos = new DataOutputStream(new FileOutputStream(file));
            dis = new DataInputStream(new FileInputStream(file));
            int readInt;

            dos.writeInt(2048);

            dos.flush();
            dos.close();

           readInt = dis.readInt();
           dis.close();
           System.out.println(readInt);
        }catch(FileNotFoundException e){
            e.printStackTrace();
            System.exit(-1);
        }catch(IOException e){
            e.printStackTrace();
            System.exit(-1);
        }









    }

    private static void testPrintIO(){
        FileWriter fw = null;
        PrintWriter pw = null;
        String path = "data" + File.separator + "log.log";

        try{
            pw = new PrintWriter(new FileWriter(path));
        }catch(IOException e){
            e.printStackTrace();
            System.exit(-1);
        }

        for(int i=0;i<10000;i++){
            pw.print(i + " ");
            if(i%100 == 0){
                pw.println();
            }
        }

        pw.println("===="+new Date()+"====");

        pw.flush();
        pw.close();
    }
    private static void testPrintStream(){
        FileOutputStream fis = null;
        PrintStream ps = null;
        String path = "data" + File.separator + "math.dat";


        try{
            ps = new PrintStream(new FileOutputStream(path));

            ps.write(new byte[]{1,2,3,4,5,6,7,8,9});

        }catch(FileNotFoundException e){
            e.printStackTrace();
            System.exit(-1);
        }catch(IOException e){
            e.printStackTrace();
            System.exit(-1);
        }



        ps.flush();
        ps.close();
    }

    private static void testObjectIO(){
        String path = "data" + File.separator + "t.obj";
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        ByteArrayOutputStream baos = null;
        TT tt = new TT(123,456,"HelloWorld");

        try{
            baos = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(baos);


            oos.writeObject(tt);
            oos.writeObject(new TT(666,888,"建国70周年"));
            oos.flush();
            oos.close();

            ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));

            System.out.println(ois.readObject());
            System.out.println(ois.readObject());
          //  System.out.println(ois.readObject());

            ois.close();
        }catch(FileNotFoundException e){
            e.printStackTrace();
            System.exit(-1);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
            System.exit(-1);
        }catch (IOException e){
            e.printStackTrace();
            System.exit(-1);
        }



    }


}

class TT implements Serializable{   //序列化标识接口
    int a;
    int b;
    String s;
    transient int k;    //transient 修饰的成员序列化不被加入

    TT(int a, int b, String s) {
        this.a = a;
        this.b = b;
        this.s = s;
        this.k = 15;

    }

    @Override
    public String toString() {
        return this.a + " " + this.b + " " + this.s + " " + this.k;
    }
}
import java.io.*;
import java.util.ArrayList;

public class IOStreamTest {
    public static void main(String[] args){
        /*FileInputStream fis = null;
        String dir = "data" + File.separator + "kkk.txt";

        try{
            fis = new FileInputStream(dir);
        }catch(FileNotFoundException e){
            System.out.println("File Not Found");
            System.exit(-1);
        }

        try{


            long sum = 0;

            for( int b = 0;(b = fis.read()) != -1;sum++){
                System.out.print((char)b);
            }

            System.out.println();
            System.out.println("共读取了" + sum + "个字符");
            fis.close(); //关闭文件输入流

        }catch(IOException e){
            System.out.println("文件读取错误");
            System.exit(-1);
        }
        */
     /*   FileReader fr = null;
        String dir = "data" + File.separator + "kkkk.txt";

        try{
            fr = new FileReader(dir);

        }catch(IOException e){
           // System.out.println("File Not Found");
            e.printStackTrace();
            System.exit(-1);
        }

        try{


            long sum = 0;

            for( int b = 0;(b = fr.read()) != -1;sum++){
                System.out.print((char)b);
            }

            System.out.println();
            System.out.println("共读取了" + sum + "个字符");
            fr.close(); //关闭文件输入流

        }catch(IOException e){
            System.out.println("文件读取错误");
            System.exit(-1);
        }*/
        String srcFileName = "data" + File.separator + "1.png";
        String descFileName = "data" + File.separator + "3.png";

        fileCopy(srcFileName,descFileName);

   //  String path = "data" + File.separator + "words.txt";

   //   writeWords(path);
    }

    private static void writeWords(String path){
     //   FileWriter fw = null;
        BufferedWriter bw = null;
        try{
           bw = new BufferedWriter(new FileWriter(path));
        }catch(IOException e){
            e.printStackTrace();
            System.exit(-1);
        }


        try{
            for(int i=0;i<65535;i++){
                bw.write(i);
            }

            bw.flush();
            bw.close();
        }catch(IOException e){
            e.printStackTrace();
            System.exit(-1);
        }
    }

    private static void fileCopy(String srcFileName,String descFileName){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        File file = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
//        BufferedInputStream bis = null;
//        BufferedOutputStream bos = null;
        long fileSize=0;

        try{
         //   fis = new FileInputStream(srcFileName);
         //   fos = new FileOutputStream(descFileName);


            file = new File(srcFileName);
            fileSize = file.length();
          //  fileData = new byte[(int)fileSize];

            fis = new FileInputStream(srcFileName);
            fos = new FileOutputStream(descFileName);
//        bis = new BufferedInputStream(new FileInputStream(file));
//        bos = new BufferedOutputStream(new FileOutputStream(descFileName));
//        System.out.println(fileSize);
        }catch(FileNotFoundException e){
            e.printStackTrace();
            System.exit(-1);
        }catch(IOException e){
            e.printStackTrace();
            System.exit(-1);
        }

        try{
            int count=0;//存放每次写出的字节
            long size=0;//累计写出字节
            byte percent=0;//存放计算百分比
            byte[] b = new byte[1024];
           long startTime = System.nanoTime();
           while((count = fis.read(b,0,b.length)) >= 0){

               //size < fileSize &&
               baos.write(b,0,count);
               // fos.write(fileData,(int)size,count);
                size += count;  //累计写出多少字节

                if(percent != (byte)((double)size/fileSize*100)){//限制同一数值百分比多次出现
                    percent = (byte)((double)size/fileSize*100);
                    System.out.println(percent + "%");
                }

            }

           System.out.println("复制所耗时间:"+(System.nanoTime()-startTime));

         //  fos.write(fileData); //将内存缓存写到文件

       //     fos.write(baos.toByteArray());

            baos.writeTo(fos);

            baos.flush();
            baos.close();

            fis.close();//关闭读入流
            fos.flush();//更新缓冲区
            fos.close();//关闭输出流
        }catch(IOException e){
            e.printStackTrace();
            System.exit(-1);
        }

    }
}

//不使用 BufferedIO所耗时间 复制所耗时间:64342480479
//使用 BufferedIO所耗时间 复制所耗时间:49165378487
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,695评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,569评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,130评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,648评论 1 297
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,655评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,268评论 1 309
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,835评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,740评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,286评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,375评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,505评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,185评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,873评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,357评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,466评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,921评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,515评论 2 359

推荐阅读更多精彩内容