缓冲流和序列化流

 FileInputStream 文件输入流
 BufferedInputStream 缓冲输入流
 FileOutputStream 文件输出流
 BufferedOutputStream 缓冲输出流
         

读取(输入)一个文件的内容

File f=new File("E:\\bookimg\\新建文本文档.txt");
        FileInputStream fis=new FileInputStream(f);
        BufferedInputStream bis=new BufferedInputStream(fis);
        byte[] bs=new byte[(int)f.length()];
        bis.read(bs);
        System.out.println(new String(bs));
        bis.close();
        fis.close();

 文件的复制

E:\\bookimg:路径需要自己建

新建文本文档2.txt:可自动生成

(不写路径会提示文件找不到)

 File f=new File("E:\\bookimg\\新建文本文档.txt");
        File f1=new File("E:\\bookimg\\新建文本文档2.txt");
        //1.先读取f的内容
        FileInputStream fis=new FileInputStream(f);
        BufferedInputStream bis=new BufferedInputStream(fis);
        byte[] bs=new byte[(int)f.length()];
        bis.read(bs);
        bis.close();
        fis.close();
        //2.将内容写入f1
        FileOutputStream fos=new FileOutputStream(f1);
        BufferedOutputStream bos=new BufferedOutputStream(fos);
        bos.write(bs);
        bos.flush();//强制清空缓冲区
        bos.close();
        fos.close();
        System.out.println("复制ok");
        **/
        
        //复制文件夹
        File f=new File("E:\\269\\swing_01");
        if(f.exists()&&f.isDirectory()) {
            copyDirectory(f, "E:\\exx");
        }
    }

 复制文件夹的方法

 /**
     * 复制文件夹的方法
     * @param f 需要复制的文件或者文件夹
     * @param path 复制的目标路径
     * @throws Exception
     */
    public static void copyDirectory(File f,String path) throws Exception{
        for (File x : f.listFiles()) {
            File y=new File(path+"\\"+x.getName());
            if(x.isFile()) {
                y.createNewFile();//对应的文件创建出来
                /* 此处省略 */
            }else {
                y.mkdir();//创建文件夹
                copyDirectory(x, y.toString());
            }
        }
    }

序列化:把对象变成文件(把程序里的数据保存到本地)

反序列化:把文件变成对象(把本地的数据读写到程序里)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小宝的宝呢

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

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

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

打赏作者

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

抵扣说明:

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

余额充值