2020.8.8三日目

21387 ワード

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

2.問題:コードが少し読めない
3.明日もJavaの勉強を続けます