一、今日学习
综合实例:写一个工具类,实现读写文件和复制文件等功能。
1 package helloworld; 2 import java.io.*; 3 public class IOUtils { 4 public static void main(String[] args) { 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 out.write(buf,0,n); 49 } 50 in.close(); 51 out.close(); 52 }catch(IOException e) { 53 e.printStackTrace(); 54 throw new RuntimeException(e); 55 } 56 } 57 public static void cpl(File from,File to) { 58 try { 59 InputStream in=new FileInputStream(from); 60 OutputStream out=new FileOutputStream(to); 61 int b; 62 while((b=in.read())!=-1) { 63 out.write(b); 64 } 65 in.close(); 66 out.close(); 67 }catch(IOException e) { 68 e.printStackTrace(); 69 throw new RuntimeException(e); 70 } 71 } 72 public static void cp(String from,String to) { 73 cp(new File(from),new File(to)); 74 } 75 /**将文件按照16进制形式打印到控制台,每16个byte为一行*/ 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 out.write(buf,0,count); 148 } 149 out.flush(); 150 } 151 }
二、遇到问题
三、明日学习
用Java语言完成C++例题