黑马程序员-IO_字节流应用
?
----------------------?android培训、java培训、期待与您交流! ----------------------
?
?
字节流
?
字符流:
FileReader
FileWriter。
字符流缓冲区:
BufferedReader
BufferedWriter
?
字节流:
InputStream?
OutputStream
字节流缓冲区:
BufferedInputStream
BufferedOutputStream
?
需求,想要操作图片数据。这时就要用到字节流。
复制一个图片.
?
import java.io.*;class FileStream{public static void main(String[] args) throws IOException{readFile_3();}//使用字符流读取文件方式3public static void readFile_3()throws IOException{FileInputStream fis = new FileInputStream("fos.txt");//int num = fis.available();//该方法获取到将要进行读取的流中的数据长度。byte[] buf = new byte[fis.available()];//定义一个刚刚好的缓冲区。不用在循环了。fis.read(buf);System.out.println(new String(buf));fis.close();}//使用字节流读取数据方法2,一次读取多个数据存入到缓冲区,一次进行打印。public static void readFile_2()throws IOException{FileInputStream fis = new FileInputStream("fos.txt");//建立byte数组,用于存放临时数据。byte[] buf = new byte[1024];int len = 0;while((len=fis.read(buf))!=-1)//将数据读取到byte数组中去,返回读取字节个数。{System.out.println(new String(buf,0,len));//将读取到数组中的数据进行打印,从0到有效个数。}fis.close();}//使用字节流读取文件中的数据打印到控制台上,一次读取一个字节public static void readFile_1()throws IOException{FileInputStream fis = new FileInputStream("fos.txt");int ch = 0;//使用read方法,一次读取一个字节的数据。while((ch=fis.read())!=-1){System.out.println((char)ch);//强转为字符类型进行打印}fis.close();}//使用字节流向文件中写入一段数据public static void writeFile()throws IOException{//创建文件字节流对象FileOutputStream fos = new FileOutputStream("fos.txt");//通过gatBytes方法将字符串转换为字节数组,然后写入到流中去。fos.write("abcde".getBytes());//注意:使用字节流不需要刷新,数据就能存入到文件中。fos.close();}}?
?
复制一个图片
思路:
1,用字节读取流对象和图片关联。
2,用字节写入流对象创建一个图片文件。用于存储获取到的图片数据。
3,通过循环读写,完成数据的存储。
4,关闭资源。
?
import java.io.*;class CopyPic{public static void main(String[] args) {FileOutputStream fos = null;FileInputStream fis = null;try{fos = new FileOutputStream("c:\\2.bmp");//创建字节输入流和输出流对象fis = new FileInputStream("c:\\1.bmp");byte[] buf = new byte[1024];//建立缓冲的数组int len = 0;//使用read方法将数据读取到数组中while((len=fis.read(buf))!=-1){fos.write(buf,0,len);//将数组中的数据写入到输出流中去。}}catch (IOException e){throw new RuntimeException("复制文件失败");}finally{try{if(fis!=null)fis.close();}catch (IOException e){throw new RuntimeException("读取关闭失败");}try{if(fos!=null)fos.close();}catch (IOException e){throw new RuntimeException("写入关闭失败");}}}}?
?
演示mp3的复制。通过缓冲区。
BufferedOutputStream
BufferedInputStream
?
import java.io.*;class CopyMp3{public static void main(String[] args) throws IOException{long start = System.currentTimeMillis();//获取时间,用于计算复制过程中消耗的时间copy_1();long end = System.currentTimeMillis();System.out.println((end-start)+"毫秒");}//使用自己写的字节流缓冲区地方法进行读取public static void copy_2()throws IOException{MyBufferedInputStream bufis = new MyBufferedInputStream(new FileInputStream("c:\\9.mp3"));BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("c:\\3.mp3"));int by = 0;//System.out.println("第一个字节:"+bufis.myRead());while((by=bufis.myRead())!=-1){bufos.write(by);}bufos.close();bufis.myClose();}//通过字节流的缓冲区完成复制。public static void copy_1()throws IOException{//创建两个流对象,并使用缓冲流进行装饰。BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("c:\\0.mp3"));BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("c:\\1.mp3"));int by = 0;//使用一次读取一个字节的方式,耗时稍多。while((by=bufis.read())!=-1){bufos.write(by);}bufos.close();bufis.close();}}?
根据BufferedInputStream中的原理,自定义一个字节缓冲流。定义一个读取方法。
?
import java.io.*;class MyBufferedInputStream{private InputStream in;private byte[] buf = new byte[1024*4];//建立缓冲区private int pos = 0,count = 0;//定义计数器和指针MyBufferedInputStream(InputStream in)//构造方法接收一个流对象{this.in = in;}//一次读一个字节,从缓冲区(字节数组)获取。public int myRead()throws IOException{//通过in对象读取硬盘上数据,并存储buf中。if(count==0){count = in.read(buf);//count是读取到的字节的个数if(count<0)return -1;//读取到文件结束返回-1pos = 0;//对指针进行初始化byte b = buf[pos];//得到一个字节count--;//计数器自减pos++;//指针自增一次return b&255;//将得到的一个字节返回,为避免连续8个一出现-1,所以&255.}else if(count>0){byte b = buf[pos];//如果缓冲区中还有数据就继续返回。知道没有为止。count--;pos++;return b&0xff;}return -1;}public void myClose()throws IOException{in.close();}}?
?
read方法读取到的是一个byte类型的数据,返回的为什么是int类型呢?
是因为字节数据在内存中是以二进制数据的形式存在的,并且不规则排列,会出现连续多个1的情况,转换为10进制以后就是-1.所以要对二进制数据进行处理。
byte: -1 ?---> ?int : -1;
00000000 00000000 00000000 11111111 :8个1是 255
类型提升以后,在前面补全1,,其值同样是-1,也不是我们想要的结果。
11111111 11111111 11111111 11111111
?
?
11111111 ?-->提升了一个int类型 那不还是-1吗?是-1的原因是因为在8个1前面补的是1导致的。
那么我只要在前面补0,即可以保留原字节数据不变,又可以避免-1的出现。
怎么补0呢?
?
?11111111 11111111 11111111 11111111 ? ? ? ? ? ? ? ? ? ? ? ?
&00000000 00000000 00000000 11111111?
------------------------------------
?00000000 00000000 00000000 11111111?
?
?
结论:
字节流的读一个字节的read方法为什么返回值类型不是byte,而是int。
因为有可能会读到连续8个二进制1的情况,8个二进制1对应的十进制是-1.
那么就会数据还没有读完,就结束的情况。因为我们判断读取结束是通过结尾标记-1来确定的。
所以,为了避免这种情况将读到的字节进行int类型的提升。
并在保留原字节数据的情况前面了补了24个0,变成了int类型的数值。
?
而在写入数据时,只写该int类型数据的最低8位。
?
?
?
?
----------------------?android培训、java培训、期待与您交流! ----------------------
?