云计算百科
云计算领域专业知识百科平台

字节缓冲流

利用字节缓冲流拷贝文件:

一次一个

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();

}
}

赞(0)
未经允许不得转载:网硕互联帮助中心 » 字节缓冲流
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!