IO_处理流_字节缓冲流
程序员文章站
2024-03-07 08:14:50
...
字节流都加上缓冲流来提升功能。
package com.sxt.io;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
/**
* 四个步骤: 分段读取 文件字节输入流 加入缓冲流
* 1、创建源
* 2、选择流
* 3、操作
* 4、释放资源
*
* @author
*
*/
public class BufferedTest01 {
public static void main(String[] args) {
File src = new File("abc.txt");
//2、选择流
InputStream is =null;
try {
is =new BufferedInputStream(new FileInputStream(src));
//3、操作 (分段读取)
byte[] flush = new byte[1024]; //缓冲容器
int len = -1; //接收长度
while((len=is.read(flush))!=-1) {
//字节数组-->字符串 (解码)
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//4、释放资源
try {
if(null!=is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void test1() {
//1、创建源
File src = new File("abc.txt");
//2、选择流
InputStream is =null;
BufferedInputStream bis =null;
try {
is =new FileInputStream(src);
bis = new BufferedInputStream(is);
//3、操作 (分段读取)
byte[] flush = new byte[1024]; //缓冲容器
int len = -1; //接收长度
while((len=is.read(flush))!=-1) {
//字节数组-->字符串 (解码)
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//4、释放资源
try {
if(null!=is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(null!=bis) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.sxt.io;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* 文件字节输出流 加入缓冲流
*1、创建源
*2、选择流
*3、操作(写出内容)
*4、释放资源
* @author
*
*/
public class BufferedTest02 {
public static void main(String[] args) {
//1、创建源
File dest = new File("dest.txt");
//2、选择流
OutputStream os =null;
try {
os =new BufferedOutputStream( new FileOutputStream(dest));
//3、操作(写出)
String msg ="IO is so easy\r\n";
byte[] datas =msg.getBytes(); // 字符串-->字节数组(编码)
os.write(datas,0,datas.length);
os.flush();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
//4、释放资源
try {
if (null != os) {
os.close();
}
} catch (Exception e) {
}
}
}
}
package com.sxt.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* 文件拷贝:文件字节输入、输出流
*
* @author
*
*/
public class Copy {
public static void main(String[] args) {
long t1 = System.currentTimeMillis();
copy("IO开篇.mp4","IO-copy.mp4");
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);
}
public static void copy(String srcPath,String destPath) {
//1、创建源
File src = new File(srcPath); //源头
File dest = new File(destPath);//目的地
//2、选择流
try( InputStream is=new BufferedInputStream(new FileInputStream(src));
OutputStream os =new BufferedOutputStream( new FileOutputStream(dest)); ) {
//3、操作 (分段读取)
byte[] flush = new byte[1024]; //缓冲容器
int len = -1; //接收长度
while((len=is.read(flush))!=-1) {
os.write(flush,0,len); //分段写出
}
os.flush();
}catch(FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}