文件复制

今天在写一下文件的复制,同时也回顾一下,文件的复制就涉及到输入流,和输出流,本人在这里用的字符流,复制的流程:
我可以理解为分为三各位区域,从一个文件的数据输入到内存区,在从内存区输入到另外一个文件,先创建一个ui界面:效果如下图:
这里写图片描述

代码如下,界面问题各位看官,小弟在此也就不多说了

这里写代码片

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import cn.huaxin.search.MyJFrame;

public class CopyJFrame extends MyJFrame implements ActionListener {
    JTextField jtextFiled;
    JTextField jtextFiled1;
    JButton button1;
    JButton button2;

    public CopyJFrame(String title) {
        super(title);

    }

    public CopyJFrame(String titel, int width, int height, boolean flag) {
        super(titel, width, height, flag);
    }

    public void myAdd() {
        jtextFiled = new JTextField();
        jtextFiled.setBounds(10, 10, 300, 30);
        add(jtextFiled);
        button1 = new JButton();
        button1.setBounds(310, 10, 100, 50);
        button1.setText("选择文件");
        button1.setActionCommand("1");
        add(button1);
        button1.addActionListener(this);
        jtextFiled1 = new JTextField();
        jtextFiled1.setBounds(10, 60, 300, 30);
        add(jtextFiled1);
        button2 = new JButton();
        button2.setText("复制文件");
        button2.setActionCommand("2");
        button2.setBounds(310, 60, 100, 50);
        button2.addActionListener(this);
        add(button2);
    }

    public void actionPerformed(ActionEvent e) {
        String commd = e.getActionCommand();
        // 对话弹窗
        JOptionPane jopp = null;
        String srcpath = null;
        if ("1".equals(commd)) {
            // 文件选择框
            JFileChooser jfc = new JFileChooser();
            // 选择框模式,文件和文件夹
            jfc.setFileSelectionMode(jfc.FILES_AND_DIRECTORIES);
            // 弹出一个 "Open File" 文件选择器对话框
            jfc.showOpenDialog(null);
            //获取选择的文件
            File file = jfc.getSelectedFile();
            //文件为空
            if (file == null) {
                jopp = new JOptionPane();
                //弹出提示对话框:
                jopp.showMessageDialog(null, "请输入路径", "提示",
                        JOptionPane.WARNING_MESSAGE);
                return;
            }
            //获取文件的路径
            srcpath = file.getAbsolutePath();

            jtextFiled.setText(srcpath);

        } else {
            CopyFile cf = new CopyFile();
            //输入框为空
            if (jtextFiled1.getText() == null
                    || "".equals(jtextFiled1.getText())) {
                // System.out.println("1111111111111");
                jopp = new JOptionPane();
                jopp.showMessageDialog(null, "请输入路径", "提示",
                        JOptionPane.WARNING_MESSAGE);
                return;

            } else {
                //复制文件
                cf.copyFileFlod(jtextFiled.getText(), jtextFiled1.getText(),
                        true);
            }

        }

    }

}

“核心代码,文件的复制
思路:
1:创建源文件File srcFile = new File(“路径”);
2: 包装成字符输入缓冲流 BufferedReader br = new BufferedReader(srcFile);
读入 String content=br.readerLine();一行行的读,读完返回null;
3: 创建目标文件 File targetFile = new File(“路径”);
4:包装成字符输出缓冲流:BufferedReader bw = new BufferedWriter(targetFile);
5.将源文件的内容写入另一个文件到中去:bw.write(content);
代码如下:

这里写代码片// 文件读取
    public void copyFile(String srcPath, String destion, boolean Append) {
        // 创建源文件
        File srcfile = new File(srcPath);
        // 目标文件
        File defile = new File(destion);
        // 文件输出缓冲区
        BufferedWriter bw = null;
        // 文件输入缓冲区
        BufferedReader br = null;
        // 先判断源文件存不存在,不存在就没意义

        if (srcfile.exists()) {

            try {
                // 文件字符输入流
                FileReader fr = new FileReader(srcfile);
                // 文件输出流
                FileWriter fw = new FileWriter(destion);
                br = new BufferedReader(fr);
                bw = new BufferedWriter(fw);

                String content = null;
                // 读取原文件,保存到 content
                while ((content = br.readLine()) != null) {
                    // 文件内容写入目标文件
                    bw.write(content);
                }

            } catch (Exception e1) {
                e1.printStackTrace();
            }

            finally {
                //
                if (br != null) {
                    try {
                        // 关闭字符输入流
                        br.close();
                    } catch (IOException e) {

                        e.printStackTrace();
                    }
                }
                if (bw != null) {
                    try {
                        bw.flush();
                        bw.close();

                    } catch (IOException e) {

                        e.printStackTrace();
                    }
                }
            }

        }
    }
二:最核心的部分,文件夹的复制:
        1:不管是源文件和源文件夹存不存在:fileSrc.exists();
        2:如果存在在判读是不是文件:fileSrc.isFile();
        3:如果是文件就调用上面文件复制函数, copyFile(String srcPath, String destion, boolean Append);
        4:否则是文件夹,如果源文件不存在 if(!filetarget.exists),创建文件夹filetarget.makdirs。
        5:获取源文件里的所有文件File[]file = filleSrc.listFile();然后遍历文件数组。
        6:获取源文件夹下的子文件File childFile = file[i];
        7:获取每个文件的路径绝对路径:chileFile.getAbsolutePath();
        8:获取每个文件的路径名:chileFile.getName();
        9:目标文件名字符串拼接:filetarget+"/"+chileFile.getName();
        10:递归,方法自己调自己


// 核心文件夹内容的读取
    public void copyFileFlod(String srcPath, String destion, boolean Append) {
        File fileSrc = new File(srcPath);
        File filedest = new File(destion);
        // 判断文件存在,不管是文件和文件夹
        if (fileSrc.exists()) {
            // 是否是文件
            if (fileSrc.isFile()) {
                // 是文件就copy
                copyFile(srcPath, destion, Append);
            } else {
                // 是文件夹进来,如果文件夹不存在就创建文件路径
                if (!filedest.exists()) {
                    // 创建文件夹目录。
                    filedest.mkdirs();
                }
                // 返回一个路径名数组,这些路径名表示此路径名表示的目录中的文件。
                File[] list = fileSrc.listFiles();
                for (int i = 0; i < list.length; i++) {
                    File childfile = list[i];
                    // 获取源文件夹孩子路径
                    String srPath = childfile.getAbsolutePath();
                    // 获取每个子文件的路径名
                    String childName = childfile.getName();
                    // 目标文件路径字符串拼接
                    String destionPath = destion + "/" + childName;
                    // 方法自己调自己。递归
                    copyFileFlod(srPath, destionPath, Append);
                }
            }

        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值