缓冲流
作者:互联网
package gangzi;
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
* 1.流的分类
* 操作数据单位不同:字节流,字符流
* 数据的流向:输入流,输出流
* 流的角色:节点流,和处理流
* 二,流的体系结构
* 抽象基类 节点流(或文件流) 缓存流(处理流的一种)
* IntputStream FileInputStream BufferedInputStream
* OutputStream FileOUtputStream BufferedOutputStream
* Reader FileReader BufferedReader
* Writer FileWriter BuffereWriter
*
*/
public class FileReaderWriterTest {
public static void main(String[] args) {
File file = new File("Helloc.txt");//相较于当前工程
System.out.println(file.getAbsolutePath());
File file1 = new File("untitled3//Helloc.txt");
}
/**
* 将untitled3包下的Helloc.txt读取,输入到控制台
* 说明点
* read()的理解;返回读入的一个字符,如果达到文章末尾,返回一个-1
* 异常的处理;为了包装资源一定可以执行关闭操作,需要使用try-catch-finally处理
* 读入的文件一定要存在,否则就会报FileNotFoundExeeption
*/
@Test
public void tesfFileReader() {
//1.实例化file类的对象,指明要操作的文件
File file = new File("Helloc.txt");//相较于当前Module
//2.提供具体的流
FileReader fr = null;
try {
fr = new FileReader(file);
//3.数据的读入
//read()返回一个数组,如果达到文章末尾 ,返回-1
/* 方式1
int date= fr.read();
while (date != -1){
System.out.print((char)date);
date = fr.read();
}*/
//方式二
int date;
while ((date = fr.read())!=-1){
System.out.print((char)date);
}
} catch (IOException e) {
e.printStackTrace();
} finally {//4.流的关闭
}
if(fr!=null){
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void testFileReader1() {
FileReader fr = null;
try {
//1.File类的实例化
File file = new File("Helloc.txt");
//2.FileReader流的实例化
fr = new FileReader(file);
//3.读入的操作
//read(char[] cbuf):返回每次读入cbuf数组中的字符个数
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf))!=-1){
/* 错误的写法
for (int i1 =0; i1<cbuf.length;i1++){
System.out.print(cbuf[i1]);
}*/
//正确的写法
// for (int i = 0; i < len; i++){
// System.out.print(cbuf[i]);
// }
//方式二错误的写法
// String s = new String(cbuf);
// System.out.print(s);
while ((len = fr.read()) != -1){
for (int i =0; i<len; i++){
System.out.print(cbuf[i]);
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null){
try {
//4.资源的关闭
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package gangzi;
import org.junit.Test;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Dome01 {
//从内存中写出数据到文件里
/*
输出操作,对应的file可以不存在
File对应硬盘中文件如果不存在在执行的过程中会自动创建文件
File对应硬盘中文件如果存在:如果流使用的构造器是FileWriter(file,false)或者FileWriter(file),那么就会肤感原文件
使用的是FileWriter(file,true)不会覆盖原文件,而是在原文件的基础上在添加写的文件
*/
@Test
public void testFileWriter() throws IOException {
//提供file类对象,用于数据的写出
File file = new File("Helloc1.txt");
//提供fileWriter的对象,用于数据的写出
FileWriter fw = new FileWriter(file,false);
//3.写出的具体操作
fw.write("\nI have a dream!\n");
fw.write("you need to have a dream!");
//4.流资源的关闭
fw.close();
}
}
@Test
public void FileInputOutream() {
FileInputStream fis= null;
FileOutputStream fos = null;
try {
File srcFile = new File("2019-01-08-gwen-stacey.jpg");
File destFile = new File("2019-01-08-gwen-stacey2.jpg");
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
byte[] cbuf = new byte[5];
int len;
while ((len = fis.read(cbuf))!=-1){
fos.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public void copy(String srcPath,String destPath){
FileInputStream fis= null;
FileOutputStream fos = null;
try {
File srcFile = new File(srcPath);
File destFile = new File(destPath);
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
byte[] cbuf = new byte[5];
int len;
while ((len = fis.read(cbuf))!=-1){
fos.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void tsstCopy(){
long start = System.currentTimeMillis();
// String srcPath="C:\\Users\\17736723817\\Desktop\\al10.mp4";
// String desPath="C:\\Users\\17736723817\\Desktop\\al11.mp4";
String srcPath="Helloc.txt";
String desPath="Hello5c.txt";
copy(srcPath,desPath);
long end = System.currentTimeMillis();
System.out.println("赋值操作"+(end-start));
}
}
package BufferedTest;
/*
处理流之一,缓冲流的使用
1.BufferedInputStream
2.BufferedOutputStram
3.BufferedReader
4.BufferedWriter
作用:提高流的读取写入的速度
*/
import org.junit.Test;
import java.io.*;
public class BufferedTest {
@Test
public void BufferedTst() {
//造文件
FileInputStream fis = null;
FileOutputStream fos = null;
try {
File sic = new File("123.png");
File sid = new File("1234.png");
//造流
//节点流
fis = new FileInputStream((sic));
fos = new FileOutputStream((sid));
//缓冲流
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
//赋值细节:读取,写入的过程
byte[] buffer = new byte[10];
int len;
while ((len = bis.read(buffer))!= -1){
bos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
//要求要先关闭外面的流在关闭里面的流,关闭外面流的同时里面的流也会自动关闭
if (fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
@Test
public void testBufferedReaderBufferedWriter(){
//创建文件和相应的流
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("4562")));
bw = new BufferedWriter(new FileWriter(new File("45621")));
//读写操作
//方式一
// char[] cbuf = new char[1024];
// int len;
// while((len = br.read(cbuf))!=-1){
// bw.write(cbuf,0,len);
// }
//方式二
String dake;
while ((dake = br.readLine()) != null){
//bw.write(dake+"\n");//date方法中不包含换行,可以加入转义字符
bw.write(dake);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (br != null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//关闭资源
}
//对图片进行加密
@Test
public void test5(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("123.png");
fos = new FileOutputStream("1236.png");
byte[] cbuf = new byte[5];
int lem;
while ((lem = fis.read(cbuf)) != -1){
for (int i = 0;i<lem;i++){
cbuf[i] = (byte)(cbuf[i]^5);
}
fos.write(cbuf,0,lem);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
;
}
//对图片进行解密
@Test
public void test7(){
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("1236.png");
fos = new FileOutputStream("1237.png");
byte[] cbuf = new byte[5];
int lem;
while ((lem = fis.read(cbuf)) != -1){
for (int i = 0;i<lem;i++){
cbuf[i] = (byte)(cbuf[i]^5);
}
fos.write(cbuf,0,lem);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
..
标签:fos,缓冲,IOException,File,new,null,cbuf 来源: https://www.cnblogs.com/java5745/p/15201519.html