createNewFile() ;FileInputStream、FileOutputStream复制文件

import java.io.File;
import java.io.IOException;

public class New01 {
    public static void main(String[] args) {
        File file = new File("d:\\xinzhi.txt");//注意添加后缀,否则报错
        try {
            boolean a =file.createNewFile();//调用创建方法
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 复制文件夹:

package tream;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//复制文件
public class Stream22 {
    public static void main(String[] args) {
        FileInputStream fis = null;//成员变量
        FileOutputStream fos = null;
        long start = System.currentTimeMillis();//输出现在时间
        try {
            fis = new FileInputStream("D:\\ZL\\微信图片_20230303114523.jpg");//输入流 读  原文件
            fos = new FileOutputStream("D:\\ZL\\aa.jpg");//输出流 写 复制的文件
            byte[] bytes = new byte[1024*1024];//以字节1024*1024速度复制
            int read = fis.read(bytes);
            while (read!=-1){//字节不为空就接着循环
                fos.write(bytes,0,read);//给复制的文件写入字节
                read = fis.read(bytes);
            }
            long end = System.currentTimeMillis();//输出复制完的时间
            System.out.println(end-start+"ms");//用了多少毫秒
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}

 

构造方法里面,只能放文件的路径,字符串形式绝对路径:从根目录下往下数相对路径:相对当前文件的路径

createNewFile()

isDirectory()

isFile()

exists()

Length()

mkdir()创建文件夹

InputStream 输入流(读)和utputStream 输出流(写)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值