美文网首页
JavaIO流问题总结

JavaIO流问题总结

作者: 手打小黑板 | 来源:发表于2020-02-13 09:33 被阅读0次
    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
    

    相关文章

      网友评论

          本文标题:JavaIO流问题总结

          本文链接:https://www.haomeiwen.com/subject/ybkkfhtx.html