java拷贝文件到另一个目录下并重命名

String pdfFileAddr="E:/phpstudy_pro/WWW/php_jr/HEMSTEST/orderPdfFile/2025/0522/czout_material/32/YCHC202503171559_271344_32_out_baoguan_material.pdf";
        File sourcefilePdf=new File(pdfFileAddr);
        
        String attFile = "E:/phpstudy_pro/WWW/php_jr/HEMSTEST/attachFiles";
        Date date = new Date();
        SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
        SimpleDateFormat mmdd = new SimpleDateFormat("MMdd");
        String fileYyyy = yyyy.format(date);
        String fileMmdd = mmdd.format(date);
        String newPath=attFile+"/"+fileYyyy + "/" + fileMmdd;
        File newFile=new File(newPath);
        if(!newFile.exists()){
            newFile.mkdirs();
        }
        
        //文件重新命名
        String relativeFullPath = (FileUtil.generateFilename(sourcefilePdf.getName()));
        System.out.println("=====relativeFullPath=>"+relativeFullPath);
        String[] arrStr=relativeFullPath.split("/");
        
        InputStream inStream = new FileInputStream(sourcefilePdf);
        OutputStream outStream=new FileOutputStream(new File(newPath,arrStr[2]));
        byte[] bytes=new byte[1024];
        int length;
        while ((length = inStream.read(bytes)) >= 0) {
            outStream.write(bytes, 0, length);
        }
        outStream.close();
        inStream.close();

public static String generateFilename(String originalFilename) {
        Date date = new Date();
        SimpleDateFormat yyyy = new SimpleDateFormat("yyyy");
        SimpleDateFormat mmdd = new SimpleDateFormat("MMdd");
        String fileYyyy = yyyy.format(date);
        String fileMmdd = mmdd.format(date);

        String fileExt = "";
        int lastIndex = originalFilename.lastIndexOf('.');

        if (lastIndex != -1) {
            fileExt = originalFilename.substring(lastIndex);
        }
        SnowFlake idWorker = new SnowFlake(0, 0);
        long id = idWorker.nextId();
        String filename = fileYyyy + "/" + fileMmdd + "/" + id + fileExt;
        return filename;
    }

要替换一个已经存在于 zip 文件中的文件,可以按照以下步骤操作: 1. 打开 zip 文件并定位到要替换的文件所在的目录。 2. 将要替换的新文件拷贝到该目录下。 3. 删除旧文件。 4. 将新文件命名为旧文件的名称。 5. 保存并关闭 zip 文件。 以下是一个简单的 Java 代码示例,演示了如何使用 ZipInputStream 和 ZipOutputStream 类来实现文件替换功能: ```java import java.io.*; import java.util.zip.*; public class ZipFileUpdater { public static void updateZipFile(String zipFilePath, String targetFileName, File newFile) throws IOException { ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath)); ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath + ".tmp")); ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { if (!entry.getName().equals(targetFileName)) { // 如果不是目标文件,则直接将该文件写入到新的 zip 输出流中 zos.putNextEntry(entry); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = zis.read(buffer)) != -1) { zos.write(buffer, 0, bytesRead); } zos.closeEntry(); } else { // 如果是目标文件,则先写入新文件,再跳过旧文件 zos.putNextEntry(new ZipEntry(entry.getName())); FileInputStream inputStream = new FileInputStream(newFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { zos.write(buffer, 0, bytesRead); } zos.closeEntry(); zis.closeEntry(); } } zis.close(); zos.close(); // 删除旧文件并重命名文件 File oldZipFile = new File(zipFilePath); oldZipFile.delete(); new File(zipFilePath + ".tmp").renameTo(oldZipFile); } } ``` 在这个示例中,我们使用 ZipInputStream 类来遍历 zip 文件中的所有条目,并使用 ZipOutputStream 类来创建一个新的 zip 文件。当找到目标文件时,我们先将新文件写入到输出流中,然后跳过旧文件。最后,我们删除旧文件并将新文件命名为旧文件的名称。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值