java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Java中的缓冲流

Java中的缓冲流详细解析

作者:端脑

这篇文章主要介绍了Java中的缓冲流详细解析,缓冲流可以分为字节缓冲流,字符缓冲流,字节缓冲流可分为字节输⼊入缓冲流,字节输出缓冲流,字符缓冲流可以分为字符输入缓冲流,字符输出缓冲流,需要的朋友可以参考下

缓冲流

在这里插入图片描述

缓冲流可以分为字节缓冲流,字符缓冲流,字节缓冲流可分为字节输⼊入缓冲流,字节输出缓冲流,字符缓冲流可以分为字符输入缓冲流,字符输出缓冲流。

在这里插入图片描述

BufferedInputStream 字节输入缓冲流

使用字节流作为基础管道的基础上,提供了一个缓冲区,在访问硬盘时缓冲区可以一次性存入多个字节,从缓冲区中将数据读入到字节流中,再到程序内存中。

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Test4 {
	public static void main(String[] args) {
		File file = new File("C:/Users/73981/Desktop/test.txt");
		InputStream inputStream;
		try {
			inputStream = new FileInputStream(file);
			BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
//			int temp = 0;
//			while((temp = bufferedInputStream.read())!=-1) {
//				System.out.println(temp);
//			}
			byte[] bytes = new byte[1024];
			int length = bufferedInputStream.read(bytes,10,10);
			for(byte byt:bytes) {
				System.out.println(byt);
			}
			bufferedInputStream.close();
			inputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

BufferedReader 字符输入缓冲流

readLine()方法可以直接读取一整行数据

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

public class Test {
	public static void main(String[] args) {
		InputStream inputStream = null;
		Reader reader = null;
		BufferedReader bufferedReader = null;
		try {
			inputStream = new FileInputStream("C:/Users/73981/Desktop/test.txt");
			reader = new InputStreamReader(inputStream);
//			Reader reader = new FileReader("/Users/southwind/Desktop/test.txt");
			bufferedReader = new BufferedReader(reader);
			String str = null;
			while((str = bufferedReader.readLine())!=null) {
				System.out.println(str);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				bufferedReader.close();
				reader.close();
				inputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class Test {
	public static void main(String[] args) {
		File file = new File("C:/Users/73981/Desktop/test.txt");
		try {
			Reader reader = new FileReader(file);
			BufferedReader bufferedReader = new BufferedReader(reader);
//			String str = "";
//			while((str = bufferedReader.readLine())!=null) {
//				System.out.println(str);
//			}
//			int temp = 0;
//			while((temp = bufferedReader.read())!=-1) {
//				System.out.println(temp);
//			}
			char[] chars = new char[1024];
			int length = bufferedReader.read(chars,10,10);
			for(char cha:chars) {
				System.out.println(cha);
			}
			bufferedReader.close();
			reader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

BufferedOutputStream 字节输出缓冲流

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Test {
	public static void main(String[] args) {
		File file = new File("C:/Users/73981/Desktop/test.txt");
		try {
			OutputStream outputStream = new FileOutputStream(file);
			BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
			String str = "字节输入缓冲流,使用字节流作为基础管道的基础上,提供了一个缓冲区,在访问硬盘时缓冲区可以一次性存入多个字节,从缓冲区中将数据读入到字节流中,再到程序内存中。";
			byte[] bytes = str.getBytes();
			bufferedOutputStream.write(bytes,10,10);
			bufferedOutputStream.flush();
			bufferedOutputStream.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

BufferedWriter 字符输出缓冲流

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class Test {
	public static void main(String[] args) {

		Writer writer = null;
		BufferedWriter bufferedWriter = null;
		try {
			writer = new FileWriter("C:/Users/73981/Desktop/test.txt");
			bufferedWriter = new BufferedWriter(writer);
			String str = "字节输入缓冲流,使用字节流作为基础管道的基础上,提供了一个缓冲区,在访问硬盘时缓冲区可以一次性存入多个字节,从缓冲区中将数据读入到字节流中,再到程序内存中。";
			bufferedWriter.write(str, 5, 15);
			bufferedWriter.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				bufferedWriter.close();
				writer.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}

到此这篇关于Java中的缓冲流详细解析的文章就介绍到这了,更多相关Java中的缓冲流内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文