Java IO输入输出流 FileWriter 字符流
字节缓冲流
//为什么要使用包装流,使用包装流是为了提高读写操作的性能。 public class Packing_flowDemo { public static void main(String[] args) throws Exception { File file = new File("file/packing_flow.txt"); //包装流的写法,缓冲区内存大小。1024*8=8192 (byte) // BufferedOutputStream packing = new BufferedOutputStream(new FileOutputStream(file, true)); // packing.write("大家好!你好吗?how are your !".getBytes()); // packing.close(); //包装流的读写操作。 BufferedInputStream outPacking = new BufferedInputStream(new FileInputStream(file)); byte[] buffer = new byte[1024]; int len = -1; while ((len = outPacking.read(buffer)) != -1) { System.out.println(new String(buffer, 0, len)); } } }
public static void main(String[] args) throws IOException {//为了代码看起来美观一些,直接抛出去 File file=new File("moves/许嵩 - 素颜 - 现场版.mp3"); File file1=new File("moves/许嵩 - 素颜.mp3"); //text(file, file1); //text2(file, file1); //text3(file, file1); text4(file, file1); } private static void text(File file,File file1) throws IOException { //节点流的方法,一个一个字节的读和写 long begin=System.currentTimeMillis(); FileInputStream in=new FileInputStream(file); FileOutputStream out =new FileOutputStream(file1); int len=-1; while((len=in.read())!=-1){ out.write(len); } in.close(); out.close(); System.out.println(System.currentTimeMillis()-begin);//5547毫秒 } private static void text2(File file,File file1) throws IOException { //缓冲流的写法,一个一个字节的读和写 long begin=System.currentTimeMillis(); BufferedInputStream in=new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(file1)); int len=-1; while(in.read()!=-1){ out.write(len); } in.close(); out.close(); System.out.println(System.currentTimeMillis()-begin);//63毫秒 } private static void text3(File file,File file1) throws IOException { //节点流的写法,一次性读取1024个字节 long begin=System.currentTimeMillis(); FileInputStream in=new FileInputStream(file); FileOutputStream out =new FileOutputStream(file1); int len=-1; byte[] buffer=new byte[1024]; while((len=in.read(buffer))!=-1){ out.write(buffer,0,len); } in.close(); out.close(); System.out.println(System.currentTimeMillis()-begin);//38毫秒 } private static void text4(File file,File file1) throws IOException { //缓冲流的写法,一次性读取1024个字节 long begin=System.currentTimeMillis(); BufferedInputStream in=new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(file1)); int len=-1; byte[] buffer=new byte[1024]; while((len=in.read(buffer))!=-1){ out.write(buffer,0,len); } in.close(); out.close(); System.out.println(System.currentTimeMillis()-begin);//4毫秒 }