Java——>字符型文件流,文件的常用操作方法

本文将文件的一些常用操作方法封装在了一个类中,欢迎交流学习

import java.io.*;

public class FileOperate {

    //展示文件或文件夹
    public void showFile(File file){
        File[] files = file.listFiles();
        if (files!= null && files.length != 0){
                for(File f: files){
                    this.showFile(f);
                }

        }
        //做自己的显示,file是一个文件或者空文件夹
        System.out.println(file.getAbsoluteFile());
    }

    //删除文件或文件夹
    public void deleteFile(File file){
        File[] files = file.listFiles();
        if (files!= null && files.length != 0){
            for(File f: files){
                this.deleteFile(f);
            }
        }
        //file是一个空文件夹或者文件时删除
        file.delete();
    }

    //遍历当前file的所有父目录
    public void ergodic(File file){
        File pFile = file.getParentFile();
        while( pFile != null){
            System.out.println(pFile.getAbsoluteFile());
            pFile = pFile.getParentFile();
        }
        //没有父则直接输出
        System.out.println(file.getAbsoluteFile());
    }

    //文件的复制
    public void copyFile(File file, String path) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            //根据传入的路径创建目标文件,getName方法获取该文件名称(没有路径),path传入的是路径
            File newFile = new File(path+"//" + file.getName());
            fis = new FileInputStream(file);
            fos = new FileOutputStream(newFile);
            byte[] b = new byte[1024];
            int count = fis.read(b);
            while(count != -1){
                fos.write(b,0,count);
                fos.flush();                //刷新缓冲区
                count = fis.read(b);
            }
            System.out.println("复制完毕!!!");
        }  catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭
            try {
                if(fis!=null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fos!=null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    /**
     * //设计一个方法   文件的复制, 将file文件的内容全部复制到path路径处的文件
     * 并且覆盖path原有的内容
     * @param file
     * @param path
     */
    public void copyFile(File file, String path){//当做file是一个文件  C://Test.txt
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //创建一个输入流
            bis = new BufferedInputStream(new FileInputStream(file));
            //创建一个新的file对象
            File newFile = new File(path+"//"+file.getName());
            bos = new BufferedOutputStream(new FileOutputStream(newFile));
            //读取文件中的信息
            byte[] b = new byte[1024];//通常创建的数组  1kb--8kb之间
            int count = bis.read(b);//1024  21有效的
            while(count!=-1) {

                bos.write(b,0,count);//将读取到的有效字节 写入
                bos.flush();
                count = bis.read(b);
            }
            System.out.println("复制完毕");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭
            try {
                if(bis!=null) {
                    bis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(bos!=null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void encryptionFile(File file, String path){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            File newFile = new File(path + "//" + file.getName());
            fis = new FileInputStream(file);
            fos = new FileOutputStream(newFile);
            byte[] b = new byte[1024];
            int count = fis.read(b);
            while( count != -1){
                //做点手脚   每一次数组的前两个元素位置互换 1024
                byte temp = b[0];
                b[0] = b[1];
                b[1] = temp;
                fos.write(b,0,count);
                fos.flush();
                count = fis.read(b);
            }
            System.out.println("加密完毕!!!");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭
            try{
                if (fis != null){
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fos != null){
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //文件夹的复制    file可以代表文件,也可以代表文件夹
    public void superCopyFile(File file, String newPath){
        //获取file的绝对路径  拼串的方式获取新文件的名字
        String oldFilePath = file.getAbsolutePath();
        String newFilePath = newPath + oldFilePath.split(":")[1];
        File newFile = new File(newFilePath);
        //判断传入的文件是文件夹还是文件
        File[] files = newFile.listFiles();
        if (files != null ){    //如果file是文件夹,则创建文件夹
            newFile.mkdir();
            System.out.println("文件夹复制完毕!!!");
            if (files.length != 0){     //再看文件夹里面是否有子元素,有子元素再递归操作子元素
                for (File f: files){
                    this.encryptionFile(f, newPath);
                }
            }

        }else{  //如果穿进来的file是文件,则文件复制
            FileInputStream fis = null;
            FileOutputStream fos = null;
            try{
                fis = new FileInputStream(file);
                fos = new FileOutputStream(newFile);
                byte[] b = new byte[1024];
                int count = fis.read(b);
                while (count != -1){
                    fos.write(b,0,count);
                    fos.flush();
                    count = fis.read(b);
                }
                System.out.println("文件复制完毕!!!");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try{
                    if (fis != null){
                        fis.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try{
                    if (fos != null){
                        fos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值