Buffered缓冲流
示例代码:
/**
* 缓冲流的使用
* 1.缓冲流
* BufferedInputStream
* BufferedOutputStream
* BufferedReader
* BufferedWriter
*
* 2。作用:提高读取和写入的速度
* 提高速度的原因:内部提供了一个缓冲区
*
* 3. 处理流就是套接在已有的流的基础上
*/
public class BufferedTest {
/*
实现非文本文件的复制
*/
public static void main(String[] args) {
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
//1.造文件
File src = new File("基础语法\沙滩.jpg");
//System.out.println(src.getAbsolutePath());
File dest = new File("基础语法\沙滩3.jpg");
//2.造流
//2.1 节点流
FileInputStream fileInputStream = new FileInputStream(src);
FileOutputStream fileOutputStream = new FileOutputStream(dest);
//2.2处理流(缓冲流)
bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
//3.复制的细节 读取 写入
byte[] buffer = new byte[10];
int len;
while ((len = bufferedInputStream.read(buffer)) != -1){
bufferedOutputStream.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedOutputStream != null)
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedInputStream != null)
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//4.资源关闭
//要求:先关闭外层流 再关闭内层流
//说明:在关闭外层流时,内层流会自动关闭 内层流的关闭可以省略
// fileInputStream.close();
// fileOutputStream.close();
}
}
/**
* 缓冲流的使用
* 1.缓冲流
* BufferedInputStream
* BufferedOutputStream
* BufferedReader
* BufferedWriter
*
* 2。作用:提高读取和写入的速度
* 提高速度的原因:内部提供了一个缓冲区
*
* 3. 处理流就是套接在已有的流的基础上
*/
public class BufferedTest {
/*
实现文本文件的复制
*/
public static void main(String[] args) {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
//创建文件和对应的流
bufferedReader = new BufferedReader(new FileReader(new File("基础语法\a.txt")));
bufferedWriter = new BufferedWriter(new FileWriter(new File("基础语法\d.txt")));
//读写文件
// //方式一
// char[] buffer = new char[1024];
// int len;
// while ((len = bufferedReader.read(buffer)) != -1){
// bufferedWriter.write(buffer,0,len);
//方式二
String data;
while ((data = bufferedReader.readLine()) != null){
//方法一
// bufferedWriter.write(data+ "
"); //data中不包含换行符 只有一行
//方法二
bufferedWriter.write(data);//data中不包含换行符
bufferedWriter.newLine(); //提供换行操作
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
try {
if (bufferedReader!= null)
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedWriter != null)
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
练习_图片的加密解密
加密:
public class PicSecret {
//图片加密
public static void main(String[] args) {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream("基础语法\沙滩.jpg");
fileOutputStream = new FileOutputStream("基础语法\沙滩secret.jpg");
byte[] buffer = new byte[20];
int len;
while ((len = fileInputStream.read(buffer)) != -1){
//对字节数据进行修改 进行加密操作
for (int i = 0; i <len ; i++) {
buffer[i] = (byte) (buffer[i] ^ 5);
}
fileOutputStream.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInputStream != null)
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileOutputStream != null)
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
解密:
public class PicSecretFree {
//图片解密
public static void main(String[] args) {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream("基础语法\沙滩secret.jpg");
fileOutputStream = new FileOutputStream("基础语法\沙滩secretfree.jpg");
byte[] buffer = new byte[20];
int len;
while ((len = fileInputStream.read(buffer)) != -1){
//对字节数据进行修改 进行加密操作
for (int i = 0; i <len ; i++) {
buffer[i] = (byte) (buffer[i] ^ 5);
}
fileOutputStream.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInputStream != null)
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileOutputStream != null)
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
练习_获取文本中每个字符出现的次数
public class WordCount {
//获取文本中每个字符出现的次数
public static void main(String[] args) {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new FileReader("基础语法\a.txt"));
bufferedWriter = new BufferedWriter(new FileWriter("基础语法\x.txt"));
//创建Map集合
Map<Character,Integer> map = new HashMap<>();
//遍历每一个字符,每一个字符出现的次数放到Map里面
int len;
while ((len = bufferedReader.read()) != -1){
//int 还原成 char
char ch = (char) len;
//判断是否是第一次出现
if (map.get(ch) == null ){
map.put(ch,1);
}else {
map.put(ch,map.get(ch)+1);
}
}
//把Map中的数据存在文件x.txt中 遍历Map 再写入数据
for (Map.Entry<Character,Integer> entry:map.entrySet()){
switch (entry.getKey()){
case ' ':
bufferedWriter.write("空格="+entry.getValue());
break;
case ' ':
bufferedWriter.write("tab键="+entry.getValue());
break;
case '
':
bufferedWriter.write("换行"+entry.getValue());
break;
case '
':
bufferedWriter.write("回车"+entry.getValue());
break;
default:
bufferedWriter.write(entry.getKey()+"="+entry.getValue());
break;
}
bufferedWriter.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedReader != null)
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedWriter != null)
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}