标签:file 参数 i/o mp3 stream style 字节 lis buffere
1.输入输出流(单字节读取)
package cn.itcast_02; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** 需求:复制文本内容到另一处(单字节复制) */ public class CopyFileDemo { public static void main(String[] args) throws IOException { //获取流对象 FileInputStream fis = new FileInputStream("a.txt"); FileOutputStream fos = new FileOutputStream("b.txt"); //先读取文本内容(input要写数据到流中用输入流)再输出 到另一处 //一个字节一个字节的读(返回的是该字节本身)读一个写一个 int i = 0; //返回值i表示字节本身 while((i=fis.read())!=-1){ //写 fos.write(i); } fis.close(); fos.close(); } }
2.输入输出流(字节数组读取)
1 package cn.itcast_02; 2 import java.io.FileInputStream; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 /** 6 需求:复制文本内容到另一处(字节数组复制) 7 */ 8 public class CopyFileDemo2 { 9 public static void main(String[] args) throws IOException { 10 //获取流对象将a.txt复制到b.txt 11 FileInputStream fis = new FileInputStream("a.txt"); 12 FileOutputStream fos = new FileOutputStream("b.txt"); 13 //定义一个字节数组 14 byte[] bs = new byte[1024]; 15 int len = 0;//带参数的read方法返回的是读取的字节个数 16 while((len=fis.read(bs))!=-1){ 17 System.out.println(len); 18 fos.write(bs,0,len); 19 } 20 fis.close(); 21 fos.close(); 22 } 23 }
3.缓冲流
1 package cn.itcast_02; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.FileInputStream; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8 9 /** 10 * 流。。。。。。。。。。 11 */ 12 public class Demo { 13 public static void main(String[] args) throws IOException { 14 long start = System.currentTimeMillis(); 15 //method1("aaa.mp3","bbb.mp3");//556毫秒 16 //method2("aaa.mp3", "bbb.mp3");// 197毫秒 17 //method3("aaa.mp3","bbb.mp3");//...毫秒 18 method4("aaa.mp3","bbb.mp3");//1454毫秒 19 long end = System.currentTimeMillis(); 20 System.out.println(end - start); 21 } 22 23 // 高效缓冲流单字节读取 24 public static void method1(String s1, String s2) throws IOException { 25 // 获取流对象 26 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(s1)); 27 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(s2)); 28 //单字节读写 29 int i =0; 30 while((i=bis.read())!=-1){ 31 bos.write(i); 32 } 33 bis.close(); 34 bos.close(); 35 } 36 37 // 高效缓冲流字节数组读取 38 public static void method2(String s1, String s2) throws IOException { 39 // 获取流对象 40 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(s1)); 41 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(s2)); 42 // 循环读取操作 43 byte[] by = new byte[1024]; 44 int len = 0; 45 while ((len = bis.read(by)) != -1) { 46 bos.write(by, 0, len); 47 } 48 bis.close(); 49 bos.close(); 50 } 51 52 // 普通流单字节读取 53 public static void method3(String s1, String s2) throws IOException { 54 FileInputStream fis = new FileInputStream(s1); 55 FileOutputStream fos = new FileOutputStream(s2); 56 //单字节读写 57 int i = 0; 58 while((i=fis.read())!=-1){ 59 fos.write(i); 60 } 61 fis.close(); 62 fos.close(); 63 } 64 65 // 普通流字节数组读取 66 public static void method4(String s1, String s2) throws IOException { 67 FileInputStream fis = new FileInputStream(s1); 68 FileOutputStream fos = new FileOutputStream(s2); 69 //字节数组的读写 70 byte[] by = new byte[1024]; 71 int len = 0; 72 while((len=fis.read(by))!=-1){ 73 fos.write(by,0,len); 74 } 75 fis.close(); 76 fos.close(); 77 } 78 79 }
标签:file 参数 i/o mp3 stream style 字节 lis buffere
原文地址:http://www.cnblogs.com/syg13129953117/p/7930143.html