如何在Java中编写新的子文件夹?

huangapple 未分类评论67阅读模式
英文:

How to write new subfolders in java?

问题

我正在使用XSLT处理一些.xml文件。

通过以下方式:


public static void main(final String[] args) throws Exception {
        final Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(
                Paths.get("/myPath/myfile.xsl").toFile()));
        new XsltTransformer(Paths.get("/mypath/source_files"),
                Paths.get("/mypath/target_files"), transformer)
                        .run();
    }

我能够转换source_files文件夹中的所有.xml文件,并将结果文件保存在target_files中。但是,如果我将一个子文件夹添加到我的source_file,例如/mypath/source_files/test,我会遇到以下错误:

Exception in thread "main" java.lang.IllegalStateException: javax.xml.transform.TransformerException: java.io.FileNotFoundException: /mypath/target_files/test/test.xml(没有这个文件或目录)

因此,源路径和目标路径必须相同。

如何解决这个问题?

英文:

I'm processing some .xml files by using XSLT.

By doing this


public static void main(final String[] args) throws Exception {
        final Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(
                Paths.get("/myPath/myfile.xsl").toFile()));
        new XsltTransformer(Paths.get("/mypath/source_files"),
                Paths.get("/mypath/target_files"), transformer)
                        .run();
    }

I'm able to transform all the .xml files from source_files folder and save the resulting files in target_files. By adding a subfolder to my source_file like /mypath/source_files/test I'm having this error:

Exception in thread "main" java.lang.IllegalStateException: javax.xml.transform.TransformerException: java.io.FileNotFoundException: /mypath/target_files/test/test.xml (No such file or directory)

So, the source path and the target path must be the same.

How to fix this issue?

答案1

得分: 0

没有核心库中命名为 XsltTransformer 的类,也没有与您在此处所写内容匹配的流行库。

也许这是您的 XsltTransformer 代码?从上下文中不太清楚。

如果假设这是您的代码,当您尝试写入一个不存在的文件时,会出现 "No such file or directory" 错误(通常情况下,这不是问题,通常会创建该文件并写入其中)... 在一个不存在的目录中。我认为您已经知道这一点,但问题缺乏细节,所以我在这里强调一下。

无论何时您在编写创建文件的代码时,而且您希望的行为是:“首先创建所需的所有目录,然后再写入文件”,以下是如何实现的:

public void writeHello(Path p) throws IOException {
    Files.createDirectories(p.getParent());
    Files.write(p, "Hello");
}

重要的部分是 createDirectories 这一行。请注意,如果 p.getParent 存在并且已经是目录,它将静默地不执行任何操作(不会抛出任何异常),这可能恰好是您想要的。因此,在写入 xslt 内容之前,只需在此添加这一行。

如果 XsltTransformer 不是您自己的代码,则有两个选择:[1] 更新您的问题,解释您正在使用的库,或者 [2] 对输入目录进行分析,递归地找到所有子目录,在目标文件夹中手动创建所有目录,然后让这个 XsltTransformer 处理它。

英文:

There's no class named XsltTransformer in the core libraries, nor are there any popular* ones that match what you've written here.

Perhaps this is your XsltTransformer code? It's not clear from context.

Assuming it is your code, you get a No such file or directory error when you attempt to write to a file that doesn't exist (ordinarily, no problem, that would normally just create the file and write to it)... in a directory that doesn't exist. I think you know that, but the question lacks detail, so I'm highlighting it here.

Whenever you are writing code that creates a file, and you want the behaviour of: "Create whatever directories you need to, first, then write to it", this is how to do that:

public void writeHello(Path p) throws IOException {
    Files.createDirectories(p.getParent());
    Files.write(p, "Hello");
}

The important bit being the createDirectories line. Note that it will silently do nothing (and doesn't throw anything) if p.getParent exists and is already a directory, which is presumably exactly what you want. So, just add that one liner before you write your xslt stuff.

If XsltTransformer is not your own code, there are two options: [1] update your question and explain what library you are using, or [2] do an analysis on your input dir yourself, find all subdirs (recursively, properly), manually make all dirs in the target folder, and then let this XsltTransformer thing at it.

答案2

得分: -1

或许在XsltTransformer路径中添加子文件夹?就像这样:

new XsltTransformer(Paths.get("/mypath/source_files/test")),
英文:

Maybe add the subfolder in XsltTransformer path? Like this:

new XsltTransformer(Paths.get("/mypath/source_files/test"),

huangapple
  • 本文由 发表于 2020年8月14日 23:49:45
  • 转载请务必保留本文链接:https://java.coder-hub.com/63416051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定