在 Java 代码中使用文件路径
我参考了这个 示例 并创建了一个 servlet 应用程序。
它与 TMP_DIR 和 TMP_DIR 完美配合。代码中提到的 DEST_DIR 路径(我在 C 盘中创建了一个名为 tmp
的文件夹,并且我还在应用程序文件夹中创建了一个名为 files
的文件夹。
现在我我正在将代码转移到 Linux 机器上,这不起作用,因为那里没有名为 C: 的驱动器,我尝试将代码的 c:\\tmp
替换为 /tmp<。 /code>(并在我的应用程序文件夹中创建了一个文件夹 tmp),但它不起作用,请告知。
I referred to this example and created a servlet application.
It works perfectly fine with the TMP_DIR & DEST_DIR paths that is mentioned in the code(I have created a folder named tmp
in C drive & also i have created a folder named files
inside my application folder.
Now I am shifting the code to a linux machine, where this does not work since there is not drive named C: there. I tried replacing the c:\\tmp
of the code to /tmp
(and created a folder tmp) inside my application folder. But its not working. How can I make this code generic? Please advise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您想要的是临时目录,请使用 http:// commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html 并查找 getTempDirectory()。这应该在任何地方都可以使用
,因此您需要系统提供的系统临时目录。您可以通过其路径或实际文件来获取它。 (IOUtils 提供了这两种方法)。这是 IOUtil 文档:
getTempDirectory
因此,如果您编写:
那么 myTempFile 将是您要写入的位置。
如果某些应用程序按名称请求目录,那么您可以使用 getTempDirectoryPath()
这将给出路径名。
在你的例子中我会写:
或使用@dogbane的方法:
我希望他们给出相同的答案。
如果您仍然感到困惑,请继续询问 - 这就是 SO 的用途!
更新:如果您正在使用文件做任何事情,那么熟悉 Apache 的 FileUtils 和 IOUtils 是值得的。有几件事 JDK 做得不好或自然而然,Apache 提供了更好的支持。
If what you want is a temporary directory use http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html and look for getTempDirectory(). This should work anywhere
So you need the System Temporary directory which is provided by the system. You can get this either through its path or through the actual File. (IOUtils provides both approaches). Here;s the IOUtil docs:
getTempDirectory
So if you write:
then myTempFile will be where you want to write to.
If some app is asking for the directory by name then you can use getTempDirectoryPath()
which will give the pathname.
In your example I would write:
or using @dogbane's approach:
I'd expect them to give the same answer.
If you are still confused keep asking - that's what SO is for!
UPDATE: If you are doing anything with Files it's worth becoming familiar with Apache's FileUtils and IOUtils. There are several things that the JDK doesn't do well or naturally and Apache gives better support.
用于保存临时文件的特定于平台的目录的位置由属性
java.io.tmpdir
定义。所以在你的代码中你可以使用:
The location of the platform-specific directory used to hold temporary files is defined by the property
java.io.tmpdir
.So in your code you can use:
使用 File.pathSeparator 来获取系统相关的路径分隔符。
编辑:
如果您只想要一个临时目录,请使用 File.createTempFile(..)
示例文件路径:http://www.exampledepot.com/egs/java.io/ConstructFilePath.html
临时文件示例:http://www.exampledepot.com/egs/java.io/CreateTempFile.html
编辑:< /strong>
在 Servlet 环境中保存文件:此处
Use File.pathSeparator to get system-dependent path separator.
Edited:
If you just want a temporary dir then use File.createTempFile(..)
Example file path: http://www.exampledepot.com/egs/java.io/ConstructFilePath.html
Example temporary file: http://www.exampledepot.com/egs/java.io/CreateTempFile.html
Edited:
Saving file in Servlet environment: here