黑马学员《IO(字节流)》
来源:互联网 发布:淘宝白色外套 编辑:程序博客网 时间:2024/05/18 00:01
---------------------------ASP.Net+Android+IOS开发、java培训、期待与您交流! ---------------------------------
一、字节流
字符流:FileReader、FileWriter。 字符流的缓冲区: BufferedReader、BufferedWriter
主要用于操作文本。
字节流:FileInputStream 、FileOutputStream 字节流的缓冲区: BufferedInputStream 、BufferedOutputStream
主要是用来操作的是媒体文件,例如:图片,音乐,电影…等。但是也可以操作文件。
注意:如果用字符流操作媒体文件的话,复制文件的话:也能复制,只是复制后的图片不能看,因为查看的编码是不一样的,不管是读还是写,都不需要刷新缓冲,除非用到了缓冲对象。
二、FileInputStream 用于读取诸如图像数据之类的原始字节流
读取文件,有3种方法
第一种:一个一个字符的读取,使用的read()方法,进行循环读取。
public static void readFile_1()throws IOException{FileInputStream fis = new FileInputStream("fos.txt");//新建读取对象,读取fos.txt文件int ch = 0;while((ch=fis.read())!=-1){System.out.println((char)ch);}fis.close();}
第二种:字节数组的读取。byte[]数组读取,然后转换成字符串返回。
public static void readFile_2()throws IOException{FileInputStream fis = new FileInputStream("fos.txt");byte[] buf = new byte[1024];int len = 0;while((len=fis.read(buf))!=-1){System.out.println(new String(buf,0,len));}fis.close();}
第三种:使用available()方法,读取到文件的总大小,然后定义相同大小的字节数组,读取一次即可,但是加入文件过大,大于了虚拟的内存的大小,那么就会加载不进去,所以最好的办法是定义恰当合适的数组,然后循环读取。
public 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();}
三、FileOutputStream 用于写入诸如图像数据之类的原始字节的流
下面用FileOutputStream和FileInputSteam进行复制文件
/*想要操作图片数据。这时就要用到字节流。复制一个图片.*/
import java.io.*;class CopyPic{<span style="white-space: pre;"></span>public static void main(String[] args) <span style="white-space: pre;"></span>{<span style="white-space: pre;"></span>FileOutputStream fos = null;<span style="white-space: pre;"></span>FileInputStream fis = null;<span style="white-space: pre;"></span>try<span style="white-space: pre;"></span>{<span style="white-space: pre;"></span>fos = new FileOutputStream("c:\\2.bmp");<span style="white-space: pre;"></span>fis = new FileInputStream("c:\\1.bmp");<span style="white-space: pre;"></span>byte[] buf = new byte[1024];<span style="white-space: pre;"></span>int len = 0;<span style="white-space: pre;"></span>while((len=fis.read(buf))!=-1)<span style="white-space: pre;"></span>{<span style="white-space: pre;"></span>fos.write(buf,0,len);<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>catch (IOException e)<span style="white-space: pre;"></span>{<span style="white-space: pre;"></span>throw new RuntimeException("复制文件失败");<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>finally<span style="white-space: pre;"></span>{<span style="white-space: pre;"></span>try<span style="white-space: pre;"></span>{<span style="white-space: pre;"></span>if(fis!=null)<span style="white-space: pre;"></span>fis.close();<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>catch (IOException e)<span style="white-space: pre;"></span>{<span style="white-space: pre;"></span>throw new RuntimeException("读取关闭失败");<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>try<span style="white-space: pre;"></span>{<span style="white-space: pre;"></span>if(fos!=null)<span style="white-space: pre;"></span>fos.close();<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>catch (IOException e)<span style="white-space: pre;"></span>{<span style="white-space: pre;"></span>throw new RuntimeException("写入关闭失败");<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>}<span style="white-space: pre;"></span>}}
四、字节流的缓冲
BufferedInputStream和BufferedOutputStream,他们增强了字节流的读取和写入效率。
复制mp3文件
/*演示mp3的复制。通过缓冲区。BufferedOutputStreamBufferedInputStream*/import java.io.*;class CopyMp3{public static void main(String[] args) throws IOException{long start = System.currentTimeMillis();//当前开始时间copy_2();long end = System.currentTimeMillis();//结束时间System.out.println((end-start)+"毫秒");//复制mp3文件用的时间差}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();<span class="comment" style="margin: 0px; padding: 0px; border: none; color: rgb(0, 130, 0); font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;">// 关闭写入缓冲对象流</span><span style="margin: 0px; padding: 0px; border: none; font-family: Consolas, 'Courier New', Courier, mono, serif; line-height: 18px;"> </span>bufis.close();//关闭读取缓冲流}}
---------------------------ASP.Net+Android+IOS开发、java培训、期待与您交流! ---------------------------------
0 0
- 黑马学员《IO(字节流)》
- 黑马学员《IO(字符流)》
- 黑马程序员---IO--字节流
- 黑马学员《IO(File类)》
- 黑马程序员:IO流之字节流
- 黑马程序员 IO流-->字节流
- 黑马程序员--IO流02--字节流
- 黑马程序员--IO流--字节流
- 黑马程序员-day19-IO流(字节流)
- 黑马程序员:IO流之字节流
- 黑马程序员--IO流之字节流
- 黑马程序员:IO流之字节流
- 黑马程序员---IO流-字节流
- 黑马程序员--IO流之字节流
- 黑马程序员-java基础 io字节流
- 黑马程序员--IO之字节流
- 黑马程序员-笔记-09-IO字节流
- 黑马程序员----【javaSE基础】IO--字节流
- 操作系统对堆棧内存管理的不同看出内存管理的本质
- iOS-size class
- 数据操纵语言(DML)
- iOS-storyboard
- iOS-OC对象模型
- 黑马学员《IO(字节流)》
- 从SSL安全传输到iOS证书安全体系2
- CocoaPods - 第三方框架管理工具
- bugzilla的Mysql中设置max_allowed_packet
- BZOJ2150【最小路径覆盖】
- 在weblogic上配置数据源
- nodeJS配置前端CSS样式编程<规范处理>
- java.lang.OutOfMemoryError: PermGen space
- 习题6-8 空间结构 UVa806