1 今天继续看书
综合实例
1 package File; 2 import java.io.*; 3 4 public class IOUtils { 5 public static void print(byte[] ary){ 6 for(int b:ary){ 7 b &= 0xff; //b=b&0xff 8 if(b<=0xf) 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 }catch (IOException e){ 21 e.printStackTrace(); 22 throw new RuntimeException(e); 23 } 24 } 25 public static Object deepCopy(Object obj){ 26 try{ 27 ByteArrayOutputStream buf=new ByteArrayOutputStream(); 28 ObjectOutputStream oos=new ObjectOutputStream(buf); 29 oos.writeObject(obj); 30 oos.close(); 31 byte[] ary=buf.toByteArray(); 32 ObjectInputStream ois=new ObjectInputStream(new ByteArrayInputStream(ary)); 33 Object o=ois.readObject(); 34 ois.close(); 35 return o; 36 }catch(Exception e){ 37 throw new RuntimeException(e); 38 } 39 } 40 public static void cp(File from,File to){ 41 try{ 42 InputStream in=new FileInputStream(from); 43 OutputStream out=new FileOutputStream(to); 44 byte[] buf=new byte[1024]; 45 int n; 46 while((n=in.read(buf))!=-1){ 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 cp1(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 out.close(); 66 }catch(IOException e){ 67 e.printStackTrace(); 68 throw new RuntimeException(e); 69 } 70 } 71 public static void cp(String from,String to){ 72 cp(new File(from),new File(to)); 73 } 74 public static void print(File file){ 75 try{ 76 InputStream in=new FileInputStream(file); 77 int b; 78 int i=1; 79 while((b=in.read())!=-1){ 80 if(b<=0xf) System.out.print("0"); 81 System.out.print(Integer.toHexString(b)+" "); 82 if(i++%16==0) System.out.println(); 83 } 84 System.out.println(); 85 in.close(); 86 }catch (IOException e){ 87 e.printStackTrace(); 88 throw new RuntimeException(e); 89 } 90 } 91 public static void print(String file){ 92 print(new File(file)); 93 } 94 public static void split(String file,int size){ 95 try{ 96 if(size<=0){ 97 throw new IllegalArgumentException("搞啥呀!"); 98 } 99 int idx=0; 100 InputStream in=new BufferedInputStream(new FileInputStream(file)); 101 OutputStream out=new BufferedOutputStream(new FileOutputStream(file+"."+idx++)); 102 int b; 103 int count=0; 104 while((b=in.read())!=-1){ 105 out.write(b); 106 count++; 107 if(count%(size*1024)==0){ 108 out.close(); 109 out=new BufferedOutputStream(new FileOutputStream(file+"."+idx++)); 110 } 111 } 112 in.close(); 113 out.close(); 114 }catch(IOException e){ 115 e.printStackTrace(); 116 throw new RuntimeException(e); 117 } 118 } 119 public static void join(String file){ 120 try{ 121 String filename=file.substring(0,file.lastIndexOf(".")); 122 String num=file.substring(file.lastIndexOf(".")+1); 123 int idx=Integer.parseInt(num); 124 OutputStream out=new FileOutputStream(filename); 125 File f=new File(filename+"."+idx++); 126 while(f.exists()){ 127 InputStream in=new FileInputStream(f); 128 in.close(); 129 f=new File(filename+"."+idx++); 130 } 131 out.close(); 132 }catch(IOException e){ 133 e.printStackTrace(); 134 throw new RuntimeException(e); 135 } 136 } 137 public static void cp(InputStream in,OutputStream out)throws IOException{ 138 byte[] buf=new byte[1024*512]; 139 int count; 140 while((count=in.read(buf))!=-1){ 141 System.out.println(count); 142 out.write(buf,0,count); 143 } 144 out.flush(); 145 } 146 }
上机练习
2 没遇到什么问题
3 明天开始在网上看java的教程