2020.8.6

一、今日学习内容

   1、综合实例:工具类

  1 package study1;
  2 import java.io.*;
  3 public class IOUtils {
  4     public static void main(byte[] ary) {
  5         //byte[] ary={41,4,-2,(byte)0xfe,(byte)0xff};
  6         for(int b:ary) {
  7             b&=0xff;
  8             if(b<=0xf)
  9                 System.out.print("0");
 10             System.out.print(Integer.toHexString(b)+" ");
 11         }
 12         System.out.println();
 13     }
 14     public static byte[] read(String file) {
 15         try {
 16             InputStream  in=new FileInputStream(file);
 17             byte[] buf=new byte[in.available()];
 18             in.read(buf);
 19             in.close();
 20             return buf;
 21         }catch(IOException e) {
 22             e.printStackTrace();
 23             throw new RuntimeException(e);
 24         }
 25     }
 26     public static Object deepCopy(Object obj) {
 27         try {
 28             ByteArrayOutputStream buf=new ByteArrayOutputStream();
 29             ObjectOutputStream oos=new ObjectOutputStream(buf);
 30             oos.writeObject(obj);
 31             oos.close();
 32             byte[] ary=buf.toByteArray();
 33             ObjectInputStream ois=new ObjectInputStream(new ByteArrayInputStream(ary));
 34             Object o=ois.readObject();
 35             ois.close();
 36             return o;
 37         }catch(Exception e) {
 38             throw new RuntimeException(e);
 39         }
 40     }
 41     public static void cp(File from,File to) {
 42         try {
 43             InputStream in=new FileInputStream(from);
 44             OutputStream out=new FileOutputStream(to);
 45             byte[] buf=new byte[1024];
 46             int n;
 47             while((n=in.read(buf))!=-1) {
 48                 //System.out.println("n:"+n);
 49                 out.write(buf,0,n);
 50             }
 51             in.close();
 52             out.close();
 53         }catch(IOException e) {
 54             e.printStackTrace();
 55             throw new RuntimeException(e);
 56         }
 57     }
 58     public static void cpl(File from,File to) {
 59         try {
 60             InputStream in=new FileInputStream(from);
 61             OutputStream out=new FileOutputStream(to);
 62             int b;
 63             while((b=in.read())!=-1) {
 64                 out.write(b);
 65             }
 66             in.close();
 67             out.close();
 68         }catch(IOException e) {
 69             e.printStackTrace();
 70             throw new RuntimeException(e);
 71         }
 72     }
 73     public static void cp(String from,String to) {
 74         cp(new File(from),new File(to));
 75     }
 76     public static void print(File file) {
 77         try {
 78             InputStream in=new FileInputStream(file);
 79             int b;
 80             int i=1;
 81             while((b=in.read())!=-1) {
 82                 if(b<=0xf)
 83                     System.out.print("0");
 84                 System.out.print(Integer.toHexString(b)+" ");
 85                 if(i++ % 16==0) {
 86                     System.out.println();
 87                 }
 88             }
 89             System.out.println();
 90             in.close();
 91         }catch(IOException e) {
 92             e.printStackTrace();
 93             throw new RuntimeException(e);
 94         }
 95     }
 96     public static void print(String file) {
 97         print(new File(file));
 98     }
 99     public static void split(String file,int size) {
100         try {
101             if(size<=0) {
102                 throw new IllegalArgumentException("搞啥呀!");
103             }
104             int idx=0;
105             InputStream in=new BufferedInputStream(new FileInputStream(file));
106             OutputStream out=new BufferedOutputStream(new FileOutputStream(file+"."+idx++));
107             int b;
108             int count=0;
109             while((b=in.read())!=-1) {
110                 out.write(b);
111                 count++;
112                 if(count % (size *1024)==0) {
113                     out.close();
114                     out=new BufferedOutputStream(new FileOutputStream(file+"."+idx++));
115                 }
116             }
117             in.close();
118             out.close();
119         }catch(IOException e) {
120             e.printStackTrace();
121             throw new RuntimeException(e);
122         }
123     }
124     public static void join(String file) {
125         try {
126             String filename=file.substring(0,file.lastIndexOf(","));
127             String num=file.substring(file.lastIndexOf(".")+1);
128             int idx=Integer.parseInt(num);
129             OutputStream out=new FileOutputStream(filename);
130             File f=new File(filename+"."+idx++);
131             while(f.exists()){
132                 InputStream in=new FileInputStream(f);
133                 cp(in, out);
134                 in.close();
135                 f=new File(filename+"."+idx++);
136             }
137             out.close();
138         }catch(IOException e) {
139             e.printStackTrace();
140             throw new RuntimeException(e);
141         }
142     }
143     public static void cp(InputStream in,OutputStream out)throws IOException{
144         byte[] buf=new byte[1024*512];
145         int count;
146         while((count =in.read(buf))!=-1) {
147             //System.out.println(count);
148             out.write(buf,0,count);
149         }
150         out.flush();
151     }
152  }

二、遇到的问题

    对这段代码有些部分不太明白

三、明日计划

    复习第十二章内容并开始用Java语言写c++的例题

posted @ 2020-08-06 15:20  小仙女W  阅读(114)  评论(0编辑  收藏  举报