概述
缓冲流
在流管道内增加缓存的数据,让我们使用流读取的文字更加的流畅
高级流—>创建通过低级流
这里只演示其中的一种,其他使用方式都是一样的
BufferedInputStream
构建方式 使用低级流构建
基本使用与低级流的方法完全一致
read() skip() available() close()
//创建缓冲流,用低级流构建
File file = new File(); //文件路径
FileInputStream fileinputstream = new FileInputStream(file); //字节型
BufferedInputStream bufferedinputstream = new BufferedInputStream(fileinputstream);//字节型缓冲
//BufferedInputStream 的使用方法和FileInputStream的方法一致
//在BufferedOutputStream写入的时候最好写flush()这个方法,在1.8版本前是需要写的,新版本做了优化可以不写
下面是我写的一个例子
File file = new File("D:\java_work\TestFile\abc.txt");
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
FileOutputStream fileOutputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
int buffers = bufferedInputStream.available();
System.out.println("大小:" + buffers);
byte[] bytes = new byte[10];
int code = bufferedInputStream.read(bytes);
while (code != -1) {
String value = new String(bytes, 0, code);
System.out.println(value);
code = bufferedInputStream.read(bytes);
}
System.out.println("----------------------------------------");
fileOutputStream = new FileOutputStream(file, true);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bufferedOutputStream.write('2');
bufferedOutputStream.flush();
int leg = bufferedInputStream.available();
byte[] b = new byte[leg];
int codes = bufferedInputStream.read(b);
String values = new String(b, 0, codes);
System.out.println("写入完成" + values);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedInputStream != null) {
bufferedInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedOutputStream != null) {
bufferedOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("-------------------------------------------------------------------------");
File file1 = new File("D:\java_work\TestFile\abc.txt");
FileReader fileReader = null;
BufferedReader bufferedReader = null;
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
try {
fileReader = new FileReader(file1);
bufferedReader = new BufferedReader(fileReader);
fileWriter = new FileWriter(file1);
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("静夜思-李白n");
bufferedWriter.write(
"床前明月光,n" +
"疑是地上霜,n" +
"举头望明月,n" +
"低头思故乡。");
bufferedWriter.flush();
char[] chars = new char[5];
int count = bufferedReader.read(chars);
while (count != -1) {
String value = new String(chars, 0, count);
System.out.print(value);
count = bufferedReader.read(chars);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedReader != null) {
bufferedReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileWriter != null) {
fileWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
运行结果如图
在硬盘中文件的内容如图
最后
以上就是懦弱小甜瓜为你收集整理的java缓冲流的全部内容,希望文章能够帮你解决java缓冲流所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复