gpt4 book ai didi

java - 写入临时目录由 : java. io.FileNotFoundException 引起

转载 作者:行者123 更新时间:2023-12-02 05:03:22 29 4
gpt4 key购买 nike

嗨,我有一个 java 应用程序,我正在尝试使用它写入 tempDir,但我仍然遇到以下异常。我承认我对编写文件不太了解,所以希望我缺少一些小东西。

Caused by: java.io.FileNotFoundException

我有以下代码。

https://code.google.com/p/sitemapgen4j/

File file = File.createTempFile(System.getProperty("java.io.tmpdir"), null);

WebSitemapGenerator wsg = new WebSitemapGenerator("http://www.example.com", file);
wsg.addUrl("http://www.example.com/index.html"); // repeat multiple times
wsg.write();

ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream outputStream = new FileOutputStream(file);
out.writeTo(outputStream);

堆栈跟踪

/var/folders/tp/jr6gqrhn7bs18szx5ck_b3qc0000gn/T/
ioc.Registry Problem writing sitemap file /var/folders/tp/jr6gqrhn7bs18szx5ck_b3qc0000gn/T/T2742577239986554001.tmp/sitemap.xml
ioc.Registry Operations trace:
ioc.Registry [ 1] Handling traditional 'action' component event request for account/admin/Index:sitemap.
ioc.Registry [ 2] Triggering event 'action' on account/admin/Index:sitemap
TapestryModule.RequestExceptionHandler Processing of request failed with uncaught exception: org.apache.tapestry5.ioc.internal.OperationException: Problem writing sitemap file /var/folders/tp/jr6gqrhn7bs18szx5ck_b3qc0000gn/T/T2742577239986554001.tmp/sitemap.xml [at classpath:com/domain/auto/pages/account/admin/AdminIndex.tml, line 32]
Caused by: java.io.FileNotFoundException: /var/folders/tp/jr6gqrhn7bs18szx5ck_b3qc0000gn/T/T2742577239986554001.tmp/sitemap.xml (Not a directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:162)
at java.io.FileWriter.<init>(FileWriter.java:90)
at com.redfin.sitemapgenerator.SitemapGenerator.writeSiteMap(SitemapGenerator.java:198)
... 105 more

最佳答案

不要传递null并且不要指定父目录;因为调用File.createTempFile(String prefix, String suffix) 在默认临时文件目录中创建一个空文件

try {
File file = File.createTempFile("something", ".tmp");
System.out.println(file.getCanonicalPath());
} catch (Exception e) {
e.printStackTrace();
}

输出(在我的机器上,每次调用都会发生变化)-

/tmp/something4693207146188341491.tmp

关于java - 写入临时目录由 : java. io.FileNotFoundException 引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28036081/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com