javaIO BufferedOutputStream和BufferedInputStream

BufferedOutputStream:缓冲字节输出流是一个高级流(处理流),与其他低级流配合使用。

BufferedOutputStream的API:

1.字段:

protected byte[] buf; //存储数据的内部缓冲区。 
protected int count;  //缓冲区中的有效字节数。此值始终处于 0 到 buf.length 范围内;元素 buf[0] 到 buf[count-1] 包含有效的字节数据。

2.构造函数:

BufferedOutputStream(OutputStream out) 
          创建一个新的缓冲输出流,以将数据写入指定的底层输出流。 
BufferedOutputStream(OutputStream out, int size) 
          创建一个新的缓冲输出流,以将具有指定缓冲区大小的数据写入指定的底层输出流。 
3.写方法,刷新方法
 void flush() 
          刷新此缓冲的输出流。 
 void write(byte[] b, int off, int len) 
          将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此缓冲的输出流。 
 void write(int b) 
          将指定的字节写入此缓冲的输出流。

4.写一个文件:

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lan.filepath.FilePath;

public class TestBufferedOutputStream
{
	public static void main(String[] args) throws IOException
	{
		String packagePath=FilePath.getSrcPackagePath(TestBufferedOutputStream.class);
		FileOutputStream out=new FileOutputStream(packagePath+"testBufferOutputStream.txt");
		BufferedOutputStream buffer=new BufferedOutputStream(out);
		byte b=20;
		String string="字符串";
		buffer.write(b);
		buffer.write(string.getBytes(), 0, string.getBytes().length);	
		buffer.flush();//刷新缓冲流
		buffer.close();
		out.close();
	}
}

运行结果:


BufferedInputStream:缓冲字节输入流,是一个高级流(处理流),与其他低级流配合使用。

BufferedInputStream的API:

1.字段

protected  byte[] buf 
          存储数据的内部缓冲区数组。 
protected  int count 
          比缓冲区中最后一个有效字节的索引大 1 的索引。 
protected  int marklimit 
          调用 mark 方法后,在后续调用 reset 方法失败之前所允许的最大提前读取量。 
protected  int markpos 
          最后一次调用 mark 方法时 pos 字段的值。 
protected  int pos 
          缓冲区中的当前位置。 

2.构造方法:

BufferedInputStream(InputStream in) 
          创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。 
BufferedInputStream(InputStream in, int size) 
          创建具有指定缓冲区大小的 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。 

其他方法:

明天补上


代码:读写例子

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lan.filepath.FilePath;

public class TestBufferedOutputStream
{
	public static void main(String[] args) throws IOException
	{
		String packagePath=FilePath.getSrcPackagePath(TestBufferedOutputStream.class);
		FileOutputStream out=new FileOutputStream(packagePath+"testBufferOutputStream.txt");
		BufferedOutputStream buffer=new BufferedOutputStream(out);
		byte b=20;
		String string="字符串";
		System.out.println("--------------写入:--------------");
		System.out.println(b);
		System.out.println(string);
		buffer.write(b);
		buffer.write(string.getBytes(), 0, string.getBytes().length);	
		buffer.flush();//刷新缓冲流
		buffer.close();
		out.close();
		System.out.println("-------------------读出:-------------------");
		FileInputStream in=new FileInputStream(packagePath+"testBufferOutputStream.txt");
		BufferedInputStream bufferedIn=new BufferedInputStream(in);
		byte btemp=(byte)bufferedIn.read();
		byte[] buf=new byte[80];//自定义一个缓冲区
		int size=0;
		String stringTemp="";
		//这里实际上调用的是父类的方法
		while((size=bufferedIn.read(buf))!=-1)
		{
			stringTemp+=new String(buf,0,size);
			
		}
		System.out.println(btemp);
		System.out.println(stringTemp);

		bufferedIn.close();
		
	}
}

使用BufferedInputStream和BufferOutputStream拷贝文件:

代码:

package com.lan.bufferOutputStream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.lan.filepath.FilePath;

public class TestBufferedCopy
{
	/**
	 * 使用BufferedOutputStream和BufferedInputStream拷贝文件.
	 * 使用默认的缓冲区大小
	 * @param srcFile 源文件的File对象
	 * @param destFile 目标文件的File对象
	 * @throws IOException 
	 */
	public static void copyFile(File srcFile,File destFile) throws IOException
	{
		if(!srcFile.exists())
		{
			throw new FileNotFoundException("文件"+srcFile+"不存在");
		}
		if(!srcFile.isFile())
		{
			throw new IllegalArgumentException(srcFile+"不是文件");
		}
		BufferedInputStream in=new BufferedInputStream(
				new FileInputStream(srcFile));
		BufferedOutputStream out=new BufferedOutputStream(
				new FileOutputStream(destFile));
		int size=0;
		//读到字节串到内部缓存,默认缓冲区的大小是8192字节
		while((size=in.read())!=-1)
		{
			out.write(size);//把读取到的字节串写入到缓冲区
			out.flush();//刷新缓冲区写入文件
		}
		in.close();
		out.close();
	}
	
	/**
	 * 把时间戳(long 毫秒数)转换为格式化时间字符串
	 * @param timeStamp long毫秒数
	 * @return 格式化时间字符串
	 */
	public static String timeStampToDateString(long timeStamp)
	{
		Date date = new Date(timeStamp);
		/*
			m  小时中的分钟数 
			s  分钟中的秒数  
			S  毫秒数  
		 */
		DateFormat format = new SimpleDateFormat("mm:ss:SS");
		String dateString=format.format(date);
		return dateString;
	}
	public static void main(String[] args)
	{
		String packagePath;
		try
		{
			long start=System.currentTimeMillis();//获取当前的时间戳
			packagePath = FilePath.getSrcPackagePath(TestBufferedCopy.class);
			copyFile(new File(packagePath+"李玉刚_刚好遇见你.mp3"),
					new File(packagePath+"李玉刚_刚好遇见你副本.mp3"));
			long end=System.currentTimeMillis();//获取复制后的时间戳
			
			System.out.println("默认的缓冲字节流(8192字节)拷贝文件用时(分钟:秒:毫秒):"
                                 +timeStampToDateString(end-start));
		} catch (UnsupportedEncodingException e1)
		{
			e1.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		
	}
}

运行结果:

(1)控制台:

默认的缓冲字节流(8192字节)拷贝文件用时(分钟:秒:毫秒):00:21:982

(2)拷贝文件目录:

刷新一下eclipse的包:


刷新后:

可见文件拷贝成功!使用播放器播放一下试试。嗯,拷贝的没有问题。




猜你喜欢

转载自blog.csdn.net/qq_21808961/article/details/80302135