代码有点混乱
不能在讲到一个内容的时候,新建一个包把本节的代码放进里面,这样代码就不会那么混乱了,每个章节都有相应的包相应的代码这样好让学习者更快掌握
不能在讲到一个内容的时候,新建一个包把本节的代码放进里面,这样代码就不会那么混乱了,每个章节都有相应的包相应的代码这样好让学习者更快掌握
2015-09-17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class IOUtil { /** * 单字节读取 * 不适合大文件,读取大文件效率很低 * @param filename * @throws IOException */ public static void printHex(String filename) throws IOException{ FileInputStream in = new FileInputStream(filename); int b; int i = 1 ; while ( (b=in.read())!= - 1 ){ if (b < 0xf ){ //单位数前面补0 System.out.print( "0" ); } System.out.print(Integer.toHexString(b)+ " " ); i++; if (i% 10 == 0 ){ System.out.println(); } } in.close(); } /** * 批量读取字节 * 读取大文件,效率很高,也是最常用的读取文件的方法 * @param filename * @throws IOException */ public static void printHexByByteArray(String filename) throws IOException { FileInputStream in = new FileInputStream(filename); byte buf[] = new byte [ 20 * 1024 ]; //从in中批量读取字节,放到buf字节数组中,从第0个位置放,最多放buf.length个,返回的是读到的字节个数 int b = in.read(buf, 0 , buf.length); //一次性读完,说明字节数组足够大 int j = 1 ; for ( int i= 0 ; i<b; i++){ if (buf[i] <= 0xf ){ System.out.print( 0 ); } System.out.print(Integer.toHexString(buf[i] & 0xff )+ " " ); //byte只有8位,而int有32位,所以 & 0xff可以清除高位上的数 if (j++% 10 == 0 ){ System.out.println(); } } in.close(); } public static void copyFileByByte(File src, File dst) throws IOException{ if (!src.exists()){ throw new IllegalArgumentException( "文件" +src+ "不存在!" ); } if (!src.isFile()){ throw new IllegalArgumentException(src+ "不是文件" ); } FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(dst); int b = 0 ; while ((b = in.read()) != - 1 ){ out.write(b); } in.close(); out.close(); } /** * FileOutputStream复制文件 * 用字节数组批量读取文件 * @param src * @param dst * @throws IOException */ public static void copyFileByByteArray (File src,File dst) throws IOException{ if (!src.exists()){ throw new IllegalArgumentException( "文件" +src+ "不存在!" ); } if (!src.isFile()){ throw new IllegalArgumentException(src+ "不是文件" ); } FileInputStream in = new FileInputStream(src); /* * 当dst不存在时,会自动创建,如果存在,删除后在创建 * 如果增加true,则不会删除原文件,会在原文件尾部写出新内容 */ FileOutputStream out = new FileOutputStream(dst,true); byte buf[] = new byte[10*1024]; int b = in.read(buf); out.write(buf, 0, b); out.flush(); in.close(); out.close(); } /** * DataOutputStream * @throws IOException */ public static void dataOutputStream() throws IOException{ String filename = "A/B.txt"; DataOutputStream out = new DataOutputStream(new FileOutputStream(filename)); out.writeInt(6); out.writeDouble(4.9); out.writeLong(8l); out.writeUTF("张基");//以utf-8写入 out.writeChars("张基");//以utf-16be写入 out.close(); } /** * DataInputStream * @throws IOException */ public static void dataInputStream() throws IOException{ String filename = "A/B.txt"; DataInputStream in = new DataInputStream(new FileInputStream(filename)); int i = in.readInt(); System.out.println(i); double j = in.readDouble(); System.out.println(j); long k = in.readLong(); System.out.println(k); String str1 = in.readUTF(); System.out.println(str1); char c = '\0'; while( (c = in.readChar() ) != '\0' ){ System.out.print(c); } in.close(); } /** * BufferedInputStream , BufferedOutputStream * 用带缓冲的字节流复制文件 * @param src * @param dst * @throws IOException */ public static void copyFileByBuffer(File src, File dst) throws IOException{ if(!src.exists()){ throw new IllegalArgumentException("文件"+src+"不存在!"); } if(!src.isFile()){ throw new IllegalArgumentException(src+"不是文件"); } BufferedInputStream in = new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dst)); int b = 0; while((b = in.read()) != -1){ out.write(b); out.flush();//刷新缓冲区 } in.close(); out.close(); } /** * InputStreamReader , OutputStreamWriter * 处理字符流 * @param filename * @throws IOException */ public static void isrAndOsw(File filename1,File filename2) throws IOException{ InputStreamReader in = new InputStreamReader(new FileInputStream(filename1),"gbk");//项目默认编码 OutputStreamWriter out =new OutputStreamWriter(new FileOutputStream(filename2),"gbk"); /*int b; while((b=in.read()) != -1){ System.out.print((char)b); } */ char buf[] = new char[8*1024]; int b; while((b=in.read(buf, 0, buf.length)) != -1){ String s = new String(buf,0,b); System.out.print(s); // System.out.println(buf); // out.write(buf,0,b); // out.flush(); out.write(s, 0, s.length()); out.flush(); } in.close(); out.close(); } /** * 文件读写流 * @param f1 * @param f2 * @throws IOException */ public static void frAndFw(File f1, File f2)throws IOException{ FileReader fr = new FileReader(f1); FileWriter fw = new FileWriter(f2,true);//如果加上true,则会在f2的末尾追加内容,不会删除原内容 char buf[] = new char[3*1024]; int c = 0; while(( c = fr.read(buf, 0, buf.length) )!=-1){ fw.write(buf, 0, c); fw.flush(); } fw.write("\n"); fw.close(); fr.close(); } /** * 字符流过滤器 * @param f1 * @param f2 * @throws IOException */ public static void brAnBwFilter(File f1, File f2,File f3) throws IOException{ BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(f1))); BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( new FileOutputStream(f2, true ))); // PrintWriter pw = new PrintWriter(f3); PrintWriter pw = new PrintWriter( new OutputStreamWriter( new FileOutputStream(f3, true ))); String line; while ( (line=br.readLine()) != null ){ //readLine()不能识别原文本中的换行 bw.write(line); bw.newLine(); //手动添加换行 bw.flush(); pw.println(line); pw.flush(); } br.close(); bw.close(); pw.close(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import java.io.File; import java.io.IOException; public class IOUtil_Test { public static void main(String[] args) throws IOException { //IOUtil.printHex("A\\B.txt"); /* long statime = System.currentTimeMillis(); IOUtil.printHexByByteArray("A\\B.txt"); System.out.println(); long endtime = System.currentTimeMillis(); System.out.println("读取时间:" + (endtime - statime) + "ms" );*/ //IOUtil.copyFileByByteArray(new File("A\\B.txt"), new File("A\\B1.txt")); /* IOUtil.dataOutputStream(); IOUtil.printHexByByteArray("A\\B.txt"); System.out.println(); */ //IOUtil.dataInputStream(); /* //1 long start1 = System.currentTimeMillis(); IOUtil.copyFileByByte(new File("A\\B1.txt"), new File("A\\bu1.txt")); long end1 = System.currentTimeMillis(); System.out.println(end1-start1); //2 long start2 = System.currentTimeMillis(); IOUtil.copyFileByBuffer(new File("A\\B1.txt"), new File("A\\bu2.txt")); long end2 = System.currentTimeMillis(); System.out.println(end2-start2); //3 long start3 = System.currentTimeMillis(); IOUtil.copyFileByByteArray(new File("A\\B1.txt"), new File("A\\bu3.txt")); long end3 = System.currentTimeMillis(); System.out.println(end3-start3);*/ //IOUtil.isrAndOsw(new File("A\\B1.txt"), new File("A\\isAndOsw.txt")); //IOUtil.frAndFw(new File("A\\B1.txt"), new File("A\\frAndFw.txt")); IOUtil.brAnBwFilter( new File( "A\\B1.txt" ), new File( "A\\brAnBwFilter.txt" ), new File( "A\\brAndBwFilter1.txt" )); } } |
上边是所有方法的类,下别是测试类
举报