Java-字节输入输出。(新手)
2024-10-08 23:23:12
参考手册:
BufferedInputStream
BufferedOutputStream
实例:
import java.io.*;
/*
* 文件的复制方式
* 1 字节流读写单个字节
* 2 字节流读写字节数组
* 3 字节流缓冲区 读写单个字节
* 4 字节流缓冲区读写字节数组
*/
public class Zjsrsclx {
public static void main(String[] args) throws Exception {
long e = System.currentTimeMillis();
File src = new File("D:\\ja.txt");
File dasc = new File("C:\\ja.txt");
long c = System.currentTimeMillis();
System.out.println(e-c);
lx4(src,dasc);
} private static void lx4(File src, File dasc) throws Exception {
// 4 字节流缓冲区读写字节数组
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dasc));
byte[] b = new byte[1024*10];
int len = 0;
while((len = bis.read(b))!=-1){
bos.write(b,0,len);
}
bis.close();
bos.close();
} private static void lx3(File src, File dasc) throws Exception {
//3 字节流缓冲区 读写单个字节
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dasc));
int len = 0;
while((len = bis.read())!=-1){
bos.write(len);
}
bis.close();
bos.close();
} private static void lx2(File src, File dasc) throws Exception {
//2 字节流读写字节数组
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dasc);
int len = 0;
byte[] b = new byte[1024*10];
while ((len = fis.read(b))!=-1){
fos.write(b,0,len);
}
fis.close();
fos.close();
} private static void lx1(File src,File dasc) throws Exception {
//1 字节流读写单个字节
//复制粘贴文件。
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(dasc); int len = 0;
while ((len = fis.read())!=-1){
fos.write(len);
}
fis.close();
fos.close();
} }
打印结果:
lx1:
lx2:
lx3:
lx4:
最新文章
- UDP异步通信
- 一些常用的Bootstrap模板资源站
- pip 豆瓣镜像使用
- maven编译项目理解
- c#经典俄罗斯方块 vs2012开发
- .net iis 域名泛解析实战
- Best MVC Practices(最优的MVC布局)
- 【转】qlv文件如何转换成mp4 怎样把下载好的qlv格式视频转换成MP4格式
- Welcome to Django!
- ffdshow 源代码分析 9: 编解码器有关类的总结
- SQL SERVER 执行动态SQL EXEC
- docker学习系列(二):使用Dockerfile创建自己的镜像
- winreg模块的使用
- 151. Reverse Words in a String(java 注意细节处理)
- SRM471
- poj1703 Find them, Catch them(带权并查集)
- poj1673 EXOCENTER OF A TRIANGLE
- linux设备驱动模型-浅析-转
- Computer Generated Angular Fisheye Projections [转]
- Entity Framework 中 Schema是什么