您好我有一个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
答案 0 :(得分:1)
不要通过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