万恶的javaeye,编辑器能不能优化下,每次修改样式都变形了。
FileInputStream
属于数据源
BufferedInputStream
属于FileInputStream的一个装饰
BufferedInputStream 有个内部缓冲区当read时会先把缓冲区填满,然后下次读取是直接从缓冲区读取。当读取的位置大于缓冲区时会再一次加载缓冲区。
read()和read(byte[] buf,
int off, int len)处理方式一样,区别在于后者一次返回多个数据,但是同样都是先放入缓冲区,然后再读取。
private void fill() throws IOException {
byte abyte0[] = getBufIfOpen();
if (markpos < 0)
pos = 0;
else if (pos >= abyte0.length)
if (markpos > 0) {
int i = pos - markpos;
System.arraycopy(abyte0, markpos, abyte0, 0, i);
pos = i;
markpos = 0;
} else if (abyte0.length >= marklimit) {
markpos = -1;
pos = 0;
} else {
int j = pos * 2;
if (j > marklimit)
j = marklimit;
byte abyte1[] = new byte[j];
System.arraycopy(abyte0, 0, abyte1, 0, pos);
abyte0 = abyte1;
}
count = pos;
int k = getInIfOpen().read(abyte0, pos, abyte0.length - pos);
if (k > 0)
count = k + pos;
}
public synchronized int read() throws IOException {
if (pos >= count) {
fill();
if (pos >= count)
return -1;
}
return getBufIfOpen()[pos++] & 255;
}
至于性能问题,我们都知道文件的物理读取性能肯定要大于内存读取,FileInputStream.read()相当于一次物理读取,而BufferedInputStream .read()大部分相当于一次内存读取。
同理FileOutputStream和BufferedOutputStream原理也是一样,也是有个缓冲区,每次write相当于写入缓冲区,当缓冲区慢了后在物理写入文件。
private void flushBuffer()
throws IOException
{
if(count > 0)
{
out.write(buf, 0, count);
count = 0;
}
}
public synchronized void write(int i)
throws IOException
{
if(count >= buf.length)
flushBuffer();
buf[count++] = (byte)i;
}
测试
public static void bufferTest() throws IOException{
BufferedInputStream is = new BufferedInputStream(new FileInputStream("d:/A.exe"));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("d:/B.exe"));
byte[] buffer = new byte[100];
System.out.println("buffer
start "+new Date());
int size = 0 ;
while((size=is.read(buffer, 0, buffer.length))!=-1){
out.write(buffer);
}
is.close();
out.close();
System.out.println("buffer end
"+new Date());
}
public static void ioTest()throws IOException{
FileInputStream is = new FileInputStream("d:/A.exe");
FileOutputStream out = new FileOutputStream("d:/C.exe");
byte[] buffer = new byte[100];
System.out.println("io start
"+new Date());
int size = 0 ;
while((size=is.read(buffer, 0, buffer.length))!=-1){
out.write(buffer);
}
is.close();
out.close();
System.out.println("io end
"+new Date());
}
文件大概有1G,用buffer大概用了20秒,用原始方式等了1分钟还没完,直接强制终止。
分享到:
相关推荐
在这个示例中,我们使用了 FileInputStream 和 BufferedInputStream 读取文件中的内容,并使用循环来统计字母 'A' 和 'a' 的出现次数。 示例 3: 统计文件中的各个字母出现次数 在第三个示例中,我们将演示如何使用...
FileInputStream和FileOutputStream BufferedInputStream 和 BufferedOutputStream DataInputStream 和 DataOutputStream ObjectInputStream和ObjectOutputStream PrintStream PushbackInputStream
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream); //创建播放器对象,把文件的缓冲流传入进去 player = new Player(bufferedInputStream); ...
java中实现复制文件和文件夹 public static void copyFile(File sourceFile,File targetFile) throws IOException{ // 新建文件输入流并对它进行缓冲 FileInputStream input = new FileInputStream(sourceFile)...
InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); 2。使用java.util.ResourceBundle类的getBundle()方法 示例: ResourceBundle rb ...
IO从大的方向上分为字节流和字符流,包括四个抽象类: 1、输入:Reader, InputStream类型的子类(字符,字节) 2、输出:Writer, OutputStream类型的子类(字符,字节) 决定使用哪个类以及它的构造进程的一般...
使用BufferedInputStream字节流,以及StringBuiler技术实现字节的逐行读取。即字节流的readline功能。BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(file));
BufferedInputStream buffer = new BufferedInputStream(new FileInputStream(music)); player = new Player(buffer); player.play(); } public static void main(String[] args) throws JavaLayerException {...
java.util.zip工具包里面的类实现文件夹的zip压缩。具体会用到FileInputStream,CheckedOutputStream,BufferedInputStream,ZipOutputStream.
常见的字节流类包括:FileInputStream、FileOutputStream 用于文件操作;ByteArrayInputStream、ByteArrayOutputStream 用于字节数组操作;BufferedInputStream、BufferedOutputStream 用于缓冲操作等。 字符流...
文件的创建,文件夹的创建,分别以FileOutputStream、FileInputStream、BufferedOutputStream、BufferedInputStream、FileWriter、FileReader、BufferedWriter、BufferedReader读取文件数据,向文件写入数据。
分析Java常用IO流,包括InputStream、OutputStream、FileInputStream、FileOutputStream、BufferedInputStream、BufferedOutputStream、Reader、Writer、InputStreamReader、OutputStreamWriter、FileReader、...
FileWriter、FileReader、CharArrayReader、CharArrayWriter、CharSequence、OutputStreamWriter、FileOutputStream、InputStreamReader、PrintWriter、BufferedReader、InputStream、FileInputStream、OutputStream...
FileInputStream FileOutputStream DataInputStream DataOutputStream BufferedInputStream BufferedOutputStream ObjectInputStream ObjectOutputStream ByteArrayInputStream ByteArrayOutputStream 【字符流】 ...
BufferedInputStream bins = new BufferedInputStream(ins);// 放到缓冲流里面 OutputStream outs = response.getOutputStream();// 获取文件输出IO流 BufferedOutputStream bouts = new BufferedOutputStream...
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry)); File file = new File(filePath + entry.getName()); //加入这个的原因是zipfile读取文件是随机读取的,这就...
DataInputStream out = new DataInputStream(new BufferedInputStream(new FileInputStream("数据存储文件路径"))) DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(...
我们使用 FileOutputStream 和 FileInputStream 类来读取和写入文件,并使用 BufferedOutputStream 和 BufferedInputStream 类来提高文件流的效率。 6. 字符串常用操作方法 在实验中,我们使用了字符串常用操作...
7. 字符数统计(满分50分) 版本1:满分15分 Write a method that counts the number of letters ...BufferedInputStream fileInput = new BufferedInputStream( new FileInputStream(new File(filename))); …. } }
读取BZip2文件的jar包 new InputStreamReader(new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(file)),true));