黑马学员《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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 摩托车被收了怎么办 摩托车的手续都怎么办 摩托车罚单掉了怎么办 行人遇到黄灯该怎么办 长辈借钱不还怎么办 不绣刚电梯轿壁有凹槽怎么办 电梯下限位故障怎么办 卫生间夏天太热怎么办 07大檐帽变形了怎么办 税务局不批发票怎么办 进项发票太多了怎么办 发票报销联丢失怎么办 发票领用簿没有怎么办 发票购买本遗失怎么办 销售方遗失发票怎么办 增值税发票发票联丢失怎么办 苹果购买发票丢失怎么办 空白增值税发票发票丢失怎么办 网购发票 领购簿怎么办 购物发票丢了怎么办 饭店客人买单要少钱怎么办 发票备注栏写错怎么办 卖房子发票丢失怎么办 发票二维码蓝票怎么办 车祸伤者出院怎么办 微信付款失败怎么办 增值税电子发票没打税号怎么办 买假出租车发票怎么办 纳税号错了怎么办 发票抬头写错怎么办 增值税发票打错顺序怎么办 发票打错了怎么办 电子发票错了怎么办 税率开高了怎么办 增值发票折叠了怎么办 播放器格式不对怎么办 发票弄上油了怎么办 快手视频快进了怎么办? 三星手机没声音怎么办 mp4不是标准格式怎么办 苹果七充电慢怎么办