利用字节缓冲流拷贝文件:
一次一个
public class BufferedStreamDemo01 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.txt"));
int ch;
while ((ch = bis.read()) != -1){
bos.write(ch);
}
bos.close();
bis.close();
}
}
一次多个
public class BufferedStreamDemo02 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy2.txt"));
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
}
评论前必须登录!
注册