java 使用FileSystem在zip文件中写入CSV文件

daolsyd0  于 2022-12-17  发布在  Java
关注(0)|答案(1)|浏览(159)

我想读取压缩文件中的CSV文件,并根据某些条件编辑记录。我使用FileSystem类创建压缩文件系统,然后编辑csv文件。我使用下面的代码读取csv文件

public static List<String> getFileList() {
        List<String> fileList = new ArrayList<>();
        ZipFile zip = null;
        try {
            zip = new ZipFile(zipFile);
            Enumeration<? extends ZipEntry> entries = zip.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (entry.getName().contains("**") && !entry.getName().contains("***")) {
                    fileList.add(entry.getName());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileList;
    }

    public static void modifyTextFileInZip() throws IOException {
        Path zipfilePath = Paths.get(zipFile);

        try (FileSystem fs = FileSystems.newFileSystem(zipfilePath, null)) {
            for (String file : getFileList()) {
                Path source = fs.getPath(file);
                String tempFileDest = source.getParent().toString() + "/" + "temp_" + source.getFileName();
                tempFileDest = tempFileDest.replace(".red", "");
                Path temp = fs.getPath(tempFileDest);
                editFiles(source, temp);
            }
        }
    }

    public static void editFiles(Path source, Path dest) {
        CSVReader reader;
        try {
            reader = new CSVReader(new InputStreamReader(Files.newInputStream(source)));
            List<String[]> records = reader.readAll();
            Iterator<String[]> iterator = records.iterator();
            
            Optional<String> fileName =
            Files.list(source.getParent())
                    .filter(file -> file.getFileName().toString().endsWith("***") &&
                            !file.getFileName().toString().endsWith("****"))
                    .map(file -> file.toString())
                    .findFirst();
            //System.out.println(fileName);
            
            while (iterator.hasNext()) {
                String[] record = iterator.next();
                String val = null;
                if("****".equals(record[5])){
                    try {
                        val = fileName.orElseThrow(() -> new RuntimeException("File Not found"));
                        val = "**" + val;
                        //System.out.println(val);
                    } catch(Exception e){
                        System.out.println("File not Found");
                    }
                    record[5] = val;
                }
                
                for(String cell: record) {
                    System.out.print(cell);
                }
                System.out.println();          
            }
            
            Files.createFile(dest);
            PrintWriter outputFile = new PrintWriter(Files.newOutputStream(dest));
            CSVWriter writer = new CSVWriter(outputFile);
            writer.writeAll(records);

            writer.close();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

错误日志=〉

java.nio.file.FileSystemException: C:\zipfile\P003.zip: The process cannot access the file because it is being used by another process.

    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:86)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at sun.nio.fs.WindowsFileSystemProvider.implDelete(WindowsFileSystemProvider.java:269)
    at sun.nio.fs.AbstractFileSystemProvider.delete(AbstractFileSystemProvider.java:103)
    at java.nio.file.Files.delete(Files.java:1126)
    at com.sun.nio.zipfs.ZipFileSystem.sync(ZipFileSystem.java:1294)
    at com.sun.nio.zipfs.ZipFileSystem.close(ZipFileSystem.java:277)
    at com.shrikant.Main.Main2.modifyTextFileInZip(Main2.java:60)
    at com.shrikant.EditCsvFilesApplication.main(EditCsvFilesApplication.java:21)

我希望能够读取和编辑文件使用inputStream从路径对象,然后将其传递给CSVReader对象。但当我传递outputStream到CSVWriter,并试图写入数据到临时文件,看不到任何变化内的zip文件。我也得到了一个。tmp(zipfstmp3381860066025396844.tmp)文件生成的每一个我运行我的程序。
我按照这个=〉https://stackoverflow.com/a/43836969/20783127来写这个程序
不知道出了什么问题,我能解释吗?
我正在尝试使用Java中的FileSystem类读取和编辑zip文件中的csv文件,但它不工作

gcuhipw9

gcuhipw91#

您没有zip.close()。最好使用 try-with-resources 语法,该语法在抛出异常或{ }内部返回时也会关闭。

try (ZipFile zip = new ZipFile(zipFile)) {
    ...

相关问题