项目差异class文件提取–>上线用

项目差异class文件提取–>上线用packagefileReader;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStre

大家好,又见面了,我是你们的朋友全栈君。

package fileReader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import javax.swing.JOptionPane;

public class DemoFilre {
    private static String MESSAGE = "";

    public static void main(String[] args) {
        String filePath = System.getProperty("user.home") + "\\Desktop\\aaa.txt";
        readTxtFile(filePath);

    }

    public static void readTxtFile(String filePath) {
        try {
            String encoding = "GBK";
            File file = new File(filePath);
            if (file.isFile() && file.exists()) { // 判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);// 考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    if(lineTxt.startsWith("==")) continue;
                    if(lineTxt.isEmpty()) continue;
                    System.out.println("D:\\workspace\\spdbSjptServer\\WebRoot\\" + lineTxt.toString());
                    // 读文件,copy
                    copyFile("D:\\workspace\\spdbSjptServer\\WebRoot\\" + lineTxt.toString(), System.getProperty("user.home") + "\\Desktop\\待上线\\" + lineTxt.toString(), true);
                }
                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }

    }

    public static boolean copyFile(String srcFileName, String destFileName,
            boolean overlay) {
        File srcFile = new File(srcFileName);

        // 判断源文件是否存在
        if (!srcFile.exists()) {
            MESSAGE = "源文件:" + srcFileName + "不存在!";
            JOptionPane.showMessageDialog(null, MESSAGE);
            return false;
        } else if (!srcFile.isFile()) {
            MESSAGE = "复制文件失败,源文件:" + srcFileName + "不是一个文件!";
            JOptionPane.showMessageDialog(null, MESSAGE);
            return false;
        }

        // 判断目标文件是否存在
        File destFile = new File(destFileName);
        if (destFile.exists()) {
            // 如果目标文件存在并允许覆盖
            if (overlay) {
                // 删除已经存在的目标文件,无论目标文件是目录还是单个文件
                new File(destFileName).delete();
            }
        } else {
            // 如果目标文件所在目录不存在,则创建目录
            if (!destFile.getParentFile().exists()) {
                // 目标文件所在目录不存在
                if (!destFile.getParentFile().mkdirs()) {
                    // 复制文件失败:创建目标文件所在目录失败
                    return false;
                }
            }
        }

        // 复制文件
        int byteread = 0; // 读取的字节数
        InputStream in = null;
        OutputStream out = null;

        try {
            in = new FileInputStream(srcFile);
            out = new FileOutputStream(destFile);
            byte[] buffer = new byte[1024];

            while ((byteread = in.read(buffer)) != -1) {
                out.write(buffer, 0, byteread);
            }
            return true;
        } catch (FileNotFoundException e) {
            return false;
        } catch (IOException e) {
            return false;
        } finally {
            try {
                if (out != null)
                    out.close();
                if (in != null)
                    in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/154306.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 测试一下博客行吗

    测试一下博客行吗;;;;;;;;;——————-iK7VUYG0yF6lS3QNNmW4Gw==tRymiHsi9AbKpr3tTFXxup1GFhuX0czs73gSv/E7b5c=uk29oXxJxAg+D0WGWLg/LaJ5+a4y4SSHbrMB4JywbGg=eIWSkIow/vo+D0WGWLg/LaJ5+a4y4SSHbrMB4JywbGg=pcL609

    2022年7月11日
    15
  • 网络爬虫——正则表达式语法

    网络爬虫——正则表达式语法正则表达式基础讲解一、什么是正则表达式在网络爬虫将网页内容爬取的时候,有一个关键的步骤就是对我们关注的信息进行提取,正则表达式就是用于信息筛选提取的强大工具,并且学习简单,所以建议大家掌握。Python正则表达式语句re.compile(“正则表达式”).findall(“原字符串”)1.匹配符普通字符 正常匹配其中的字符。\n 匹配换行符。\t 匹配制表符。\…

    2022年7月15日
    13
  • FileInputStream路径「建议收藏」

    FileInputStream路径「建议收藏」最近在整理之前的项目时遇到的一个问题,关于FileInputStream的路径无法确定的一个小问题。在网上找了一堆方法,自己也尝试了之后发现,用下面的代码查看一下当前的路径再确定String中应该写什么路径。System.out.println(newFile(“.”).getAbsolutePath());…

    2022年5月9日
    157
  • Unable to AUTH to MASTER: -ERR Client sent AUTH, but no password is set「建议收藏」

    Unable to AUTH to MASTER: -ERR Client sent AUTH, but no password is set「建议收藏」问题描述:redis主从复制时,想要增加密码认证功能。初学了解不多,修改了master的redis_6379.conf,增加”masterauth 123456″,同时修改slave的redis_6380.conf,增加”masterauth 123456″。认为主从配置的认证密码的参数都是masterauth。重新启动后用redis-cli连接slave,输入info replicati…

    2022年6月13日
    56
  • python如何安装sklearn库

    python如何安装sklearn库1.正常的安装思路是win+Rcmdpipinstall+所要装的库然后就会这样2.其实在我换了3.8版本之后在安装python库的时候,基本上pipinstall+库名80%是会成功的,对于这种失败的,我用了第二种方法进行安装。在官网https://www.lfd.uci.edu/~gohlke/pythonlibs/#找到库对应的文件进行下载下载好了之后可以在浏览器的“下载内容”找到我的建议是点“在文件夹中显示”然后在文件夹中选中复制切回c…

    2022年10月17日
    0
  • YUM常用命令介绍

    YUM常用命令介绍

    2021年10月8日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号