您的位置:首页 > 编程语言 > Java开发

java实现简单的IO字节流读写操作

2017-05-16 17:01 459 查看
package com.yang.stream;

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;

public class FileDemo {
public static void main(String[] args) {
String oldpath = "d:/a.txt";
String newpath = "d:/b.txt";
BufferedInputStream in = null;
BufferedOutputStream os = null;
try {
in = new BufferedInputStream(new FileInputStream(new File(oldpath)));
os = new BufferedOutputStream(new FileOutputStream(new File(newpath)));
byte[] b = new byte[1024];
int len = 0;
while((len=in.read(b))!=-1){
os.write(b, 0, len);
}
os.flush();
} catch (FileNotFoundException e) {
System.out.println("文件不存在");
} catch (IOException e) {
System.out.println("输入输出异常");
} finally {
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("文件传输成功");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: