Java文件读取与拷贝

读取文件

字符流 :字符流读取文本文件(字符流重点掌握它即可)

public static void main(String[] args) throws IOException {
    File file = new File("F:\\安装jdk.txt");
    FileReader reader = new FileReader(file);
    BufferedReader br = new BufferedReader(reader);
    String line = null;
    while((line = br.readLine()) != null){
        System.out.println(line);
    }
    br.close();
}

字节流
读取普通文件

      //以字节流读取文件
        File file1 = new File("F:\\Workspaces\\downfile\\src\\main\\resources\\test.txt");
        File file2 = new File("F:\\Workspaces\\test.txt");
        if(!file1.exists() && file1.isFile()){
            file1.createNewFile();
        }
        if(!file2.exists() && file2.isFile()){
            file2.createNewFile();
        }
        FileInputStream fis = new FileInputStream(file1);
        FileOutputStream ops = new FileOutputStream(file2);
        int len=-1;
        byte [] buff=new byte[64];
        while ((len=fis.read(buff))!=-1){
            ops.write(buff,0,len);
        }
        ops.flush();
        ops.close();
        fis.close();
    }

拷贝视频 :以字节流实现

  1. 方式一(自定义缓存大小)
 @Test
    public void  readFile() throws IOException {
        //以字节流读取文件
        File file1 = new File("F:\\video\\test.ts");
        File file2 = new File("F:\\Workspaces\\yuan.ts");
        if(!file1.exists() && file1.isFile()){
            file1.createNewFile();
        }
        if(!file2.exists() && file2.isFile()){
            file2.createNewFile();
        }
        FileInputStream fis = new FileInputStream(file1);
        FileOutputStream ops = new FileOutputStream(file2);
        int len=-1;
        byte [] buff=new byte[64];
        while ((len=fis.read(buff))!=-1){
            ops.write(buff,0,len);
        }
        ops.flush();
        ops.close();
        fis.close();
    }
  1. 方式二
public static void main(String[] args) throws IOException {//psvm
    File sources = new File("F:\\乐购 实体类 数据库工具类 编写.wmv");
    File aim = new File("F:\\乐购 实体类 数据库工具类 aim.wmv");

    FileInputStream fis = new FileInputStream(sources);
    BufferedInputStream bis = new BufferedInputStream(fis);
    FileOutputStream fos = new FileOutputStream(aim);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    
    
    byte[] buf = new byte[256];
    int len =-1;
    while ((len = bis.read(buf)) != -1){
        bos.write(buf,0,len);
    }
    bos.flush();
    bos.close();
    bis.close();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

缘不易

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值