在 Java 代码中使用文件路径

发布于 2024-09-29 23:52:13 字数 434 浏览 0 评论 0原文

我参考了这个 示例 并创建了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一梦等七年七年为一梦 2024-10-06 23:52:13

如果您想要的是临时目录,请使用 http:// commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html 并查找 getTempDirectory()。这应该在任何地方都可以使用

,因此您需要系统提供的系统临时目录。您可以通过其路径或实际文件来获取它。 (IOUtils 提供了这两种方法)。这是 IOUtil 文档:
getTempDirectory

public static File getTempDirectory()

    Returns a File representing the system temporary directory.

    Returns:
        the system temporary directory.

因此,如果您编写:

File myTempFile = IOUtils.getTempDirectory();

那么 myTempFile 将是您要写入的位置。

如果某些应用程序按名称请求目录,那么您可以使用 getTempDirectoryPath()
这将给出路径名。

在你的例子中我会写:

private static final String TMP_DIR_PATH = IOUtils.getTempDirectoryPath();

或使用@dogbane的方法:

private static final String TMP_DIR_PATH = System.getProperty("java.io.tmpdir");

我希望他们给出相同的答案。

如果您仍然感到困惑,请继续询问 - 这就是 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

public static File getTempDirectory()

    Returns a File representing the system temporary directory.

    Returns:
        the system temporary directory.

So if you write:

File myTempFile = IOUtils.getTempDirectory();

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:

private static final String TMP_DIR_PATH = IOUtils.getTempDirectoryPath();

or using @dogbane's approach:

private static final String TMP_DIR_PATH = System.getProperty("java.io.tmpdir");

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.

习ぎ惯性依靠 2024-10-06 23:52:13

用于保存临时文件的特定于平台的目录的位置由属性java.io.tmpdir 定义。

所以在你的代码中你可以使用:

private static final String TMP_DIR_PATH = System.getProperty("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:

private static final String TMP_DIR_PATH = System.getProperty("java.io.tmpdir")
最舍不得你 2024-10-06 23:52:13

使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文