java中的IO流可以分为两种:字符流和字节流
字符流,顾名思义,就是对字符进行操作,只能操作文本文件
字节流,就是对字节进行操作,然而所有文件都是由字节组成的,可以字节流可以操作一切文件
字符流中的两个大类:Reader和Writer
详情可见 http://blog.csdn.net/noble510520/article/details/50083555 简单的Writer和Reader
字节流中也同样有两个大类:InputStream和OutputStream
又“读进来,写出去”,所以InputStream对应着Reader,OutputStream对应着Writer
字节流和字符流有很多相像的地方,区别就是字节流操作字节,字符流操作字符
OutputStream
OutputStream的主要方法
构造方法
以FileOutputStream为例
FileOutputStream(String file)
FileOutputStream(File file)
FileOutputStream(String file,boolean append)
FileOutputStream(File file,boolean appeand)
后面两种方法用于判断是否需要续写,前面两个方法是直接覆盖文件
写
write(int charnum)
write(byte[] array)
write(byet[] array,int off,int len)写出array中角标从off开始的len个元素
刷新
flush()
close()
InputStream
构造方法
照惯例,以FileInputStream为例
FileInputStream(String filename)
FileInputStream(File filename)
如果filename.exists==false||filename.isFile()==false的话,那么会抛出FileNotFoundException
读
read():int
read(byte[] array):int
与Reader类一样,如果到了文件末尾,返回-1
这里有个特别好用的方法,可以用来知道文件的大小
available():int; 返回文件的字节数
这时就可以用这个方法来定义array的大小,那么就可以一次性读完了
关流
flush()
缓冲技术
缓冲技术就是把信息分批处理,再一起丢出去,这样处理速度会快很多!!
下面实现一下缓冲技术
实现BufferedInputStream
package mypackage;
import java.util.*;
import java.io.*;
public class MyBufferedInputStream {
private InputStream input;
private int index=0;
private byte[] array;
private int len=0;
public MyBufferedInputStream(FileInputStream input){this(input,1024*100);}
public MyBufferedInputStream(FileInputStream input,int size){
this.input=input;
array=new byte[size];
}
public int read()throws IOException{
if(len==0){
index=0;
if((len=input.read(array))==-1){return -1;}
}
len--;
return (array[index++]&255);
}
public int read(byte[] bytes)throws IOException{
int i;
int index=0;
while((i=read())!=-1){
bytes[index]=(byte)i;
}
return index+1;
}
public void close()throws IOException{input.close();}
}
实现BufferedOutputStream
package mypackage;
import java.util.*;
import java.io.*;
public class MyBufferedOutputStream{
private OutputStream output;
public MyBufferedOutputStream(FileOutputStream output)throws IOException{this(output,1024*100);}
public MyBufferedOutputStream(FileOutputStream output,int size)throws IOException{
this.output=output;
buf=new byte[size];
}
protected byte[] buf;
private int index=0;
public void write(byte[] bytes)throws IOException{
for(int i=0;i<bytes.length;){
buf[index++]=bytes[i++];
if(index==buf.length){
index=0;
output.write(buf);
this.flush();
buf=new byte[buf.length];
}
}
}
public void write(int bytes)throws IOException{
if(index<buf.length){buf[index++]=(byte)bytes;}
else{
output.write(buf);
output.flush();
buf=new byte[buf.length];
index=0;
buf[index++]=(byte)bytes;
}
}
public void close()throws IOException{output.flush();output.close();}
public void flush()throws IOException{output.flush();}
}
下面复制一个11.1M的MP3格式测试一下效果
import java.io.*;
import mypackage.*;
class Test{
public static void main(String[] agrs)throws IOException{
long begin=System.currentTimeMillis();
FileInputStream fi=new FileInputStream("D:\\CloudMusic\\薛之谦 - 一半.mp3");
FileOutputStream fo=new FileOutputStream("D:\\CloudMusic\\一半1.mp3");
MyBufferedInputStream bi=new MyBufferedInputStream(fi);
MyBufferedOutputStream bo=new MyBufferedOutputStream(fo);
int bytes;
byte[] array=new byte[1024*100];
while((bytes=fi.read(array))!=-1){
bo.write(array);
}
bo.close();
bi.close();
long end=System.currentTimeMillis();
System.out.println("复制所用时间:"+(end-begin)+"毫秒");
}
}
下面是结果
C:\Users\钟绍威\Desktop>java Test
复制所用时间:183毫秒
用Java自带的BufferedInputStream和BufferedOutputStream试试
import java.io.*;
class Demo{
public static void main(String[] args)throws IOException{
long begin=System.currentTimeMillis();
FileInputStream fi=new FileInputStream("D:\\CloudMusic\\薛之谦 - 一半.mp3");
FileOutputStream fo=new FileOutputStream("D:\\CloudMusic\\一半1.mp3");
BufferedInputStream bi=new BufferedInputStream(fi);
BufferedOutputStream bo=new BufferedOutputStream(fo);
byte[] a=new byte[1024*8];
for(int i;(i=bi.read(a))!=-1;){
bo.write(a);
}
bo.close();
bi.close();
long end=System.currentTimeMillis();
System.out.println("复制所用时间:"+(end-begin)+"毫秒");
}
}
下面是结果
C:\Users\钟绍威\Desktop>java Demo
复制所用时间:117毫秒
Tips:用write(byte[])会比write(int)快得多多
输入的缓冲就是先把数据存在数组中,从数组中一个个读到控制台
输出的缓冲就是把数据存到数组中,再一起写到OutputStream中的缓冲区,最后在刷新
刚刚用这个复制一个11.1M的MP3花了0.6秒,和系统的时间差不多↖(^ω^)↗!!
错误的返回了-1
如果扫描到了11111111那么此时将byte->int是-1,如果这样的话,程序就会终止不会进行
为什么read()返回的是Int型而不是byte型呢??
1int=4byte
那么11111111转为Int就是11111111 11111111 11111111 11111111 还是等于-1
所以为了防止出现这个情况,就只保留后面八位,前面用0去补
于是乎11111111&255
<script type="text/javascript">
$(function () {
$('pre.prettyprint code').each(function () {
var lines = $(this).text().split('\n').length;
var $numbering = $('<ul/>').addClass('pre-numbering').hide();
$(this).addClass('has-numbering').parent().append($numbering);
for (i = 1; i <= lines; i++) {
$numbering.append($('<li/>').text(i));
};
$numbering.fadeIn(1700);
});
});
</script>
分享到:
相关推荐
识字表一年级上册字帖.pdf
人教版二年级(上册)汉字+笔顺+识字带拼音+练习word版.doc
直映认字识字卡片.doc
# 0-5岁幼儿认字识字教育开发系统 这个系统旨在帮助0-5岁的幼儿学习认字和识字。它是一个基于Java语言开发的系统,提供了各种学习工具和资源来帮助幼儿更好地学习。 该系统包括幼儿学习成长计划,这是一种针对不同...
2018新人教版一年级语文下册生字拼音注音练习(识字+生字)大全.docx
儿童识字类软件,声文并茂,利于儿童学习。使用了很多的动画效果,涉及常用字的识记
部编版 (五四制)一年级上册语文 识字加油站+我的发现(教案).doc
详细设计主要包括系统数据库访问的实现,主要功能模块的具体实现,模块实现关键代码等。最后对系统进行功能测试,并对测试结果进行分析总结。 包括程序毕设程序源代码一份,数据库一份,完美运行。配置环境里面有...
悟空识字软件 通过动漫 故事 让孩子引发学习兴趣 不断提高识字能力
熊猫识字幼儿识字软件是一款可以帮助幼儿进行识字辨认的强大软件,下载使用熊猫识字免费版可以让您体验到最方便最便捷的识字体验,马上下载熊猫识字破解版进行操作使用吧。
小巧 简单 好用 识别率比较高的识字软件 。 ‘’
资源内容:python识字插件(完整代码+说明文档+exe程序).rar 代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 适用对象:python学习者。 作者介绍:某大厂资深算法工程师,从事Matlab、Python...
双捷识字写字宝软件是一款非常好用且功能强大的儿童识字写字软件,双捷识字写字软件填补了该类儿童识字软件仅能在电视或者电脑上演示的空白,软件功能丰富,可以支持...让孩子科学识字,从识字第一天起就规范认字、规范
多认字,容易认字,让孩子轻松学习汉字便于学习更多的知识。
智能型幼儿识字网站的设计与实现毕业论文.doc
熊猫识字-幼儿识字软件是熊猫乐园专门为3-8岁小朋友早期阅读和上小学准备的儿童识字游戏软件,整个识字游戏使用科学的多元识字法,在看动画故事、玩识字游戏过程中学汉字,比传统幼儿识字教学效果快3至10倍,同时...
毕设论文--能智型幼儿识字网站的设计与实现.doc
汉语识字量测试采用了字字测的“精准测”的方式。选中为认识,字的底色变为红色,再点击则为取消,彻底解决,用纸质纸测试时机械枯燥的问题。电脑自动随时合计识字数量,更是省了人工数数合计的工作量。其中最大特色...
Java实现QQ屏幕截图功能.zip Java实现QQ屏幕截图功能.zip Java实现QQ屏幕截图功能.zip
看图识字小程序源码.zip