代码有点混乱
不能在讲到一个内容的时候,新建一个包把本节的代码放进里面,这样代码就不会那么混乱了,每个章节都有相应的包相应的代码这样好让学习者更快掌握
不能在讲到一个内容的时候,新建一个包把本节的代码放进里面,这样代码就不会那么混乱了,每个章节都有相应的包相应的代码这样好让学习者更快掌握
2015-09-17
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(); } }
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")); } }
上边是所有方法的类,下别是测试类
举报