String的replaceAll方法替换目标为File.separator抛出异常原因

本文详细介绍了在Java中如何正确地将字符串中的特定字符进行替换,特别是当遇到需要将'/'替换为操作系统的文件分隔符时,避免因反斜杠()和美元符号($)导致的异常。通过实际案例演示了正确的替换方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为文件分隔符需要统一的问题,今天遇见了要将String中的"/"全部替换成File.separator,可是这行代码却抛出了异常
java.lang.IllegalArgumentException: character to be escaped is missing

String url = "/index/start.html"
url.replaceAll("/",File.separator);

后面仔细观察,在查看了文档后才发现了问题所在,文档中有说==如果是替换字符串中的反斜杠( \ )和美元符号( $ )可能会导致结果与被视为文字替换字符串时的结果不同;==仔细想想在Windows下的File.separator正好就是,可是因为没有直接写反斜杠,反而没有看出问题。

String url = "/index/start.html"
url.replaceAll("/","\\\\");

只要改为上面那个形式就可以正常替换了

List<LoreRepository> loreRepositories = loreRepositoryMapper.selectByIds(ids); Map<LoreRepository, List<AttachmentDTO>> loreRepositoryMap = new HashMap<>(); for (LoreRepository loreRepository : loreRepositories) { Long id = loreRepository.getId(); List<LoreRepositoryFile> loreRepositoryFiles = loreRepositoryFileService.listByLoreId(id); List<Long> annexIdList = loreRepositoryFiles.stream().map(LoreRepositoryFile::getAnnexId).collect(Collectors.toList()); List<AttachmentDTO> attachmentDTOS = attachmentsService.queryAttachmentList(annexIdList, CATEGORY_CODE); if (attachmentDTOS != null && attachmentDTOS.size() > 0) { loreRepositoryMapper.updateDownloadNum(loreRepository); loreRepositoryMap.put(loreRepository, attachmentDTOS); } } if (loreRepositoryMap.isEmpty()) { throw new BaseException("所选知识条目中,无可下载附件!"); } ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream()); loreRepositoryMap.forEach((loreRepository, attachmentDTOS) -> { attachmentDTOS.forEach(attachmentDTO -> { Response feignResponse = this.attachmentFeignClient.download(attachmentDTO.getAttFileId()); Response.Body body = feignResponse.body(); String loreTitle = loreRepository.getLoreTitle(); try (InputStream inputStream = body.asInputStream()) { zipOutputStream.putNextEntry(new ZipEntry(loreTitle + File.separator + RandomUtils.nextInt(1, 100) + "_" + attachmentDTO.getFileName())); byte[] buf = new byte[2 * 1024]; int len; while ((len = inputStream.read(buf)) != -1) { zipOutputStream.write(buf, 0, len); } zipOutputStream.closeEntry(); } catch (IOException e) { log.error("知识库ZIP文件生成失败!%s", loreRepository.getLoreTitle(), e); throw new BaseException(String.format("知识库ZIP文件生成失败!%s", loreRepository.getLoreTitle())); } }); }); 这个方法为什么下载下来的压缩文件损坏呢
03-26
public String exportAll(String importFilePath, String opType, String path, String localUrl){ setCommon(importFilePath); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { List<LeafStruExportResp> leafStruExportResps = null; if (null != this.masterList ) { leafStruExportResps = this.masterList; } if (null == leafStruExportResps || leafStruExportResps.size() == 0) { throw new HyException("No Data Found", "未找到数据"); } String address = leafStruExportResps.get(0).getScMateriAddress(); String level = leafStruExportResps.get(0).getScMateriLevel(); String remark = leafStruExportResps.get(0).getRemark(); int index = 0; List<Date> Times = new ArrayList<>(); List<String> CheckTimes = new ArrayList<>(); for (LeafStruExportResp leaf : leafStruExportResps){ Date time = leaf.getCheckDate(); Times.add(time); for (ScBakeWaterItems sc : leaf.getScBakeWaterItems()){ String checkTime = sc.getCheckTime(); CheckTimes.add(checkTime); } } Date minDate = Collections.min(Times); Date maxDate = Collections.max(Times); Double maxWaterVal = null; Double maxTemperature = null; Double maxFirstWeight = null; Double maxSecondWeight = null; Double maxSackWeight = null; Double maxWeight = null; Double minWaterVal = null; Double minTemperature = null; Double minFirstWeight = null; Double minSecondWeight = null; Double minSackWeight = null; Double minWeight = null; Double WaterVal = null; Double Temperature = null; Double FirstWeight = null; Double SecondWeight = null; Double SackWeight = null; Double Weight = null; sheetXSSF.getRow(1).getCell(6).setCellValue(minDate + "~" + maxDate); for (int i = 0; i < leafStruExportResps.size(); i++){ List<ScBakeWaterItems> sc = leafStruExportResps.get(i).getScBakeWaterItems(); sheetXSSF.getRow(5 + index).getCell(0).setCellValue(leafStruExportResps.get(i).getCheckDate()); sheetXSSF.getRow(5 + index).getCell(1).setCellValue(leafStruExportResps.get(i).getClassesName()); for (ScBakeWaterItems scBakeWaterItems : sc) { sheetXSSF.getRow(5 + index).getCell(2).setCellValue(scBakeWaterItems.getCheckTime()); sheetXSSF.getRow(5 + index).getCell(3).setCellValue(scBakeWaterItems.getSequences()); sheetXSSF.getRow(5 + index).getCell(4).setCellValue(scBakeWaterItems.getWaterVal()); sheetXSSF.getRow(5 + index).getCell(5).setCellValue(scBakeWaterItems.getTemperature()); sheetXSSF.getRow(5 + index).getCell(6).setCellValue(scBakeWaterItems.getFirstWeight()); sheetXSSF.getRow(5 + index).getCell(7).setCellValue(scBakeWaterItems.getSecondWeight()); sheetXSSF.getRow(5 + index).getCell(8).setCellValue(scBakeWaterItems.getSackWeight()); sheetXSSF.getRow(5 + index).getCell(9).setCellValue(scBakeWaterItems.getWeight()); if (null != scBakeWaterItems.getAppearance()) { if (0 == scBakeWaterItems.getAppearance()) { sheetXSSF.getRow(index + 5).getCell(10).setCellValue("合格"); } if (1 == sc.get(i).getAppearance()) { sheetXSSF.getRow(index + 5).getCell(10).setCellValue("不合格"); } } if (maxWaterVal == null || maxWaterVal < scBakeWaterItems.getWaterVal()) { maxWaterVal = scBakeWaterItems.getWaterVal(); } if (maxTemperature == null || maxTemperature < scBakeWaterItems.getTemperature()) { maxTemperature = scBakeWaterItems.getTemperature(); } if (maxFirstWeight == null || maxFirstWeight < scBakeWaterItems.getFirstWeight()) { maxFirstWeight = scBakeWaterItems.getFirstWeight(); } if (maxSecondWeight == null || maxSecondWeight < scBakeWaterItems.getSecondWeight()) { maxSecondWeight = scBakeWaterItems.getSecondWeight(); } if (maxSackWeight == null || maxSackWeight < scBakeWaterItems.getSackWeight()) { maxSackWeight = scBakeWaterItems.getSackWeight(); } if (maxWeight == null || maxWeight < scBakeWaterItems.getWeight()) { maxWeight = scBakeWaterItems.getWeight(); } if (minWaterVal == null || minWaterVal > scBakeWaterItems.getWaterVal()) { minWaterVal = scBakeWaterItems.getWaterVal(); } if (minTemperature == null || minTemperature > scBakeWaterItems.getTemperature()) { minTemperature = scBakeWaterItems.getTemperature(); } if (minFirstWeight == null || minFirstWeight > scBakeWaterItems.getFirstWeight()) { minFirstWeight = scBakeWaterItems.getFirstWeight(); } if (minSecondWeight == null || minSecondWeight > scBakeWaterItems.getSecondWeight()) { minSecondWeight = scBakeWaterItems.getSecondWeight(); } if (minSackWeight == null || minSackWeight > scBakeWaterItems.getSackWeight()) { minSackWeight = scBakeWaterItems.getSackWeight(); } if (minWeight == null || minWeight > scBakeWaterItems.getWeight()) { minWeight = scBakeWaterItems.getWeight(); } WaterVal += scBakeWaterItems.getWaterVal(); Temperature += scBakeWaterItems.getTemperature(); FirstWeight += scBakeWaterItems.getFirstWeight(); SecondWeight += scBakeWaterItems.getSecondWeight(); SackWeight += scBakeWaterItems.getSackWeight(); Weight += scBakeWaterItems.getWeight(); index++; // 插入行:从insertRowIndex开始,将后续行向下移动numRowsToInsert行 sheet.shiftRows(6 + index, sheet.getLastRowNum(), 1); // 在插入的位置创建新行并填充数据 HSSFRow newRow = sheet.createRow(6 + index); } } sheetXSSF.getRow(9 + index).getCell(4).setCellValue(index); sheetXSSF.getRow(9 + index).getCell(5).setCellValue(index); sheetXSSF.getRow(9 + index).getCell(6).setCellValue(index); sheetXSSF.getRow(9 + index).getCell(7).setCellValue(index); sheetXSSF.getRow(9 + index).getCell(8).setCellValue(index); sheetXSSF.getRow(9 + index).getCell(9).setCellValue(index); sheetXSSF.getRow(9 + index).getCell(10).setCellValue(index); sheetXSSF.getRow(10 + index).getCell(4).setCellValue(maxWaterVal); sheetXSSF.getRow(10 + index).getCell(5).setCellValue(maxTemperature); sheetXSSF.getRow(10 + index).getCell(6).setCellValue(maxFirstWeight); sheetXSSF.getRow(10 + index).getCell(7).setCellValue(maxSecondWeight); sheetXSSF.getRow(10 + index).getCell(8).setCellValue(maxSackWeight); sheetXSSF.getRow(10 + index).getCell(9).setCellValue(maxWeight); sheetXSSF.getRow(10 + index).getCell(10).setCellValue("-"); sheetXSSF.getRow(11 + index).getCell(4).setCellValue(minWaterVal); sheetXSSF.getRow(11 + index).getCell(5).setCellValue(minTemperature); sheetXSSF.getRow(11 + index).getCell(6).setCellValue(minFirstWeight); sheetXSSF.getRow(11 + index).getCell(7).setCellValue(minSecondWeight); sheetXSSF.getRow(11 + index).getCell(8).setCellValue(minSackWeight); sheetXSSF.getRow(11 + index).getCell(9).setCellValue(minWeight); sheetXSSF.getRow(11 + index).getCell(10).setCellValue("-"); sheetXSSF.getRow(12 + index).getCell(4).setCellValue(WaterVal / index); sheetXSSF.getRow(12 + index).getCell(5).setCellValue(Temperature / index); sheetXSSF.getRow(12 + index).getCell(6).setCellValue(FirstWeight / index); sheetXSSF.getRow(12 + index).getCell(7).setCellValue(SecondWeight / index); sheetXSSF.getRow(12 + index).getCell(8).setCellValue(SackWeight / index); sheetXSSF.getRow(12 + index).getCell(9).setCellValue(Weight / index); sheetXSSF.getRow(12 + index).getCell(10).setCellValue("-"); //标准差 sheetXSSF.getRow(14 + index).getCell(3).setCellValue(address); sheetXSSF.getRow(14 + index).getCell(4).setCellValue(level); sheetXSSF.getRow(14 + index).getCell(5).setCellValue(remark); OutputStream out = null; String accessUrl = ""; //修改模板内容导出新模板 if ("query".equals(opType)) { String fileName = FileUtil.getDirPath(path); File file = new File(path + File.separator + fileName); out = new FileOutputStream(file); accessUrl = localUrl + File.separator + fileName; } else { out = response.getOutputStream(); } workbookXSSF.write(out); out.close(); accessUrl = accessUrl.replaceAll("\\\\","/"); return accessUrl; } catch (Exception e) { log.info(e.getMessage()); log.error("Export Excel Error", e.getMessage()); throw new HyException("System error","生成报表异常"); } }优化代码
最新发布
08-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值