1 public class Test{ 2 3 private final static int BUFFER_SIZE = 16 * 1024; 4 5 public static void main(String[] args){ 6 File src = new File("d:\\src.txt"); 7 File dst = new File("d:\\dst.txt"); 8 doSaveFile(src, dst); 9 } 10 11 //单个字节的读写 12 public static void doSaveFile1(File src, File dst){ 13 InputStream in = null; 14 OutputStream out = null; 15 try{ 16 in = new FileInputStream(src); 17 out = new FileOutputStream(dst); 18 int len = 0; 19 while ((len = in.read()) != -1){//in.read()返回int,len表示所读取字节的int表达方式,每次读取一个字节的二进制数据 20 out.write(len); 21 } 22 }catch (Exception e){ 23 } 24 finally{ 25 if (null != in){ 26 try{ 27 in.close(); 28 } 29 catch (IOException e){ 30 } 31 } 32 if (null != out){ 33 try{ 34 out.close(); 35 } 36 catch (IOException e){ 37 } 38 } 39 } 40 } 41 42 //数据块的读写 43 public static void doSaveFile2(File src, File dst){ 44 InputStream in = null; 45 OutputStream out = null; 46 try{ 47 in = new FileInputStream(src); 48 out = new FileOutputStream(dst); 49 //int sizeLen=in.available();//此方法是返回这个流中有多少个字节数,可以把数组长度定为这个 50 byte[] buffer = new byte[BUFFER_SIZE]; 51 int len = 0; 52 while ((len = in.read(buffer)) > 0){//len表示读取的字节数,in.read(buffer)表示从in流中读取字节放入buffer中并返回字节的长度 53 out.write(buffer, 0, len); 54 //String s=new String(buffer,"utf-8"); 55 //System.out.println(s); 56 } 57 } 58 catch (Exception e){ 59 } 60 finally{ 61 if (null != in){ 62 try{ 63 in.close(); 64 } 65 catch (IOException e){ 66 } 67 } 68 if (null != out){ 69 try{ 70 out.close(); 71 } 72 catch (IOException e){ 73 } 74 } 75 } 76 } 77 78 }
InputStream的三个read的区别
read() : 从输入流中读取数据的下一个字节,返回0到255范围内的int字节值。如果因为已经到达流末尾而没有可用的字节,则返回-1。在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞。
read(byte[] b) : 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。以整数形式返回实际读取的字节数。在输入数据可用、检测到文件末尾或者抛出异常前,此方法一直阻塞。
如果 b 的长度为 0,则不读取任何字节并返回 0;否则,尝试读取至少一个字节。如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少读取一个字节并将其存储在 b 中。
将读取的第一个字节存储在元素 b[0] 中,下一个存储在 b[1] 中,依次类推。读取的字节数最多等于b 的长度。设 k 为实际读取的字节数;这些字节将存储在 b[0] 到 b[k-1] 的元素中,不影响 b[k] 到b[b.length-1] 的元素。
1.read
这个方法是对这个流一个一个字节的读,返回的int就是这个字节的int表示方式
以下是代码片段,经过测试当eclipse的编码为gbk时,转化出的字符串不需经过重新编码,如果eclipse的编码为utf-8时则由byte转成字符串需重新编成utf-8的
InputStream in = Test.class.getResourceAsStream("/tt.txt");
byte[]tt=new byte[15];//测试用的事前知道有15个字节码
while(in.available()!=0){
for(int i=0;i<15;i++){
tt[i]=(byte)in.read();
}
}
String ttttt=new String(tt,"utf-8");
System.out.println(ttttt);
in.close();
2.read(byte[] b)
这个方法是先规定一个数组长度,将这个流中的字节缓冲到数组b中,返回的这个数组中的字节个数,这个缓冲区没有满的话,则返回真实的字节个数,到未尾时都返回-1
in = Test.class.getResourceAsStream("/tt.txt");
byte [] tt=new byte[1024];
int b;
while((b=in.read(tt))!=-1){
System.out.println(b);
String tzt=new String(tt,"utf-8");
System.out.println(tzt);
}
3.read(byte[] b, int off, int len)
此方法其实就是多次调用了read()方法
InputStream in = Test.class.getResourceAsStream("/tt.txt");
//System.out.println(in.available());//此方法是返回这个流中有多少个字节数,可以把数组长度定为这个
byte[]tt=new byte[in.available()];
int z;
while((z=in.read(tt, 0, tt.length))!=-1){
System.out.println(new String(tt,"utf-8"));
}
内容摘自:
http://www.cnblogs.com/pengyingh/articles/2507207.html
http://swiftlet.net/archives/1363