标签:基础语法 abs stat red 换行 oid trace ace new
示例代码:
/**
* 缓冲流的使用
* 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+ "\n"); //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();
}
}
}
}
标签:基础语法 abs stat red 换行 oid trace ace new
原文地址:https://www.cnblogs.com/qiudajiang/p/13278972.html