Java笔记 IO流

file类方法
public File(String pathname) :通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
public File(String parent, String child) :从父路径名字符串和子路径名字符串创建新的 File实例。
public File(File parent, String child) :从父抽象路径名和子路径名字符串创建新的 File实例。
public String getAbsolutePath() :返回此File的绝对路径名字符串。
public String getPath() :将此File转换为路径名字符串。
public String getName() :返回由此File表示的文件或目录的名称。
public long length() :返回由此File表示的文件的长度。流是计算机中数据的流动

流是计算机数据的流动
为了提高效率一个流配有一个缓冲区
字节流:处理字节数据
字符流:处理字符数据
基本输出输入类
InputStream与OutputStream
常用方法
public int read()
public int red(byte[] b)
public int availble()
puiblic voidc write(int b)
文件拷贝

public class main {
    public static void main(String[] args) {
        String dir = "C:\\Users\\ycl\\Desktop";
        File file = new File(dir+"\\a");
        //拿到所有文件
        File [] files = file.listFiles();
        for(File from: files){
            String fileName = from.getName();
            copy(from.getAbsolutePath(),dir+"\\b\\"+fileName);
        }
        System.out.println("ssss");
    }
    public static void copy(String from,String to){
        try{
            //拿到上一层目录
            File targetDir = new File(new File(to).getParent());
            if (!targetDir.exists()){
                targetDir.mkdir();
            }
            FileInputStream fis = new FileInputStream(from);
            BufferedInputStream bis = new BufferedInputStream(fis);
            FileOutputStream fos = new FileOutputStream(to);
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            byte[] buf = new byte[1024];
            int size = 0;
            while ((size = bis.read(buf))!=-1){
                bos.write(buf);
            }
            bis.close();
            bos.close();
        }catch (Exception e){

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值