【问题标题】:IFile from String in javajava中来自String的IFile
【发布时间】:2020-08-18 10:18:40
【问题描述】:

我有一个包含文件名和文件内容的 Map。我想为地图中的每个条目创建 IFile。

Map<String, String> fileMap = new HashMap<>();
fileMap.put("test1.txt", "Content of test1");
fileMap.put("test2.txt", "Content of test2");

如何为每个条目创建 IFile?

【问题讨论】:

  • 这些文件在工作区中吗? IFile 只能表示工作区文件。
  • 不,这些文件不在我的工作区中。
  • 那么您就不能使用 IFile 或大多数其他 Eclipse 资源 API。你想对这些文件做什么?
  • 我想创建一个包含这些文件的 zip。
  • 好吧,如果它们不在工作区中,您只需使用普通的 Java 文件 API,例如 Path、File、ZipFile...

标签: java eclipse file eclipse-plugin


【解决方案1】:

关于您在 cmets 中所说的话,这应该可以完成工作:

    Map<String, String> fileMap = new HashMap<>();
    fileMap.put("test1.txt", "Content of test1");
    fileMap.put("test2.txt", "Content of test2");

    FileOutputStream fos = new FileOutputStream("multiCompressed.zip");
    ZipOutputStream zipOut = new ZipOutputStream(fos);
    for (Map.Entry<String, String> file : fileMap.entrySet()) {
        File fileToZip = new File(file.getKey());
        FileWriter writer = new FileWriter(fileToZip);
        writer.write(file.getValue());
        FileInputStream fis = new FileInputStream(fileToZip);
        ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
        zipOut.putNextEntry(zipEntry);

        byte[] bytes = new byte[1024];
        int length;
        while ((length = fis.read(bytes)) >= 0) {
            zipOut.write(bytes, 0, length);
        }
        fis.close();
    }
    zipOut.close();
    fos.close();
}

【讨论】:

  • 这不能回答问题的IFile 部分
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
  • 1970-01-01
  • 2018-03-18
  • 2016-05-01
相关资源
最近更新 更多