加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

字节流复制文件-单字节

发布时间:2021-05-20 09:45:13 所属栏目:大数据 来源: https://blog.csdn.net/summoxj
导读:import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* ?* 将d:demoa.txt复制到d:demob.txt ?* 字节输入流,绑定数据源??????? 字节输出流,绑定数据目的 ?* 输入,读取1个字节?? 输出,写1个字节 ?*/ p
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/*
?* 将d:demoa.txt复制到d:demob.txt
?* 字节输入流,绑定数据源??????? 字节输出流,绑定数据目的
?* 输入,读取1个字节?? 输出,写1个字节
?*/
public class Copy {
?? ?public static void main(String[] args) {
?? ??? ?//定义两个流的对象变量
?? ??? ?FileInputStream fis=null;
?? ??? ?FileOutputStream fos=null;
?? ??? ?try{
?? ??? ??? ?//建立两个流的对象,绑定数据源和数据目的
?? ??? ??? ?fis=new FileInputStream("d:demoa.txt");
?? ??? ??? ?fos=new FileOutputStream("d:demob.txt");
?? ??? ??? ?//字节输入流,读取1个字节,输出流写一个字节
?? ??? ??? ?int len=0;
?? ??? ??? ?while((len=fis.read())!=-1){
?? ??? ??? ??? ?fos.write(len);
?? ??? ??? ?}
?? ??? ?}catch(IOException ex){
?? ??? ??? ?System.out.println(ex);
?? ??? ??? ?throw new RuntimeException("文件复制失败");
?? ??? ?}finally{
?? ??? ??? ?try {
?? ??? ??? ??? ?if(fos!=null)
?? ??? ??? ??? ??? ?fos.close();
?? ??? ??? ?} catch (IOException ex) {
?? ??? ??? ??? ?throw new RuntimeException("释放资源失败");
?? ??? ??? ?}finally{
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?if(fis!=null)
?? ??? ??? ??? ??? ??? ?fis.close();
?? ??? ??? ??? ?} catch (IOException ex) {
?? ??? ??? ??? ??? ?throw new RuntimeException("释放资源失败");
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?}
}

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读