获取应用程序路径以创建新文件。

4

如何将项目的应用程序路径获取到一个字符串变量LogPath中?稍后,LogPath将用于创建该项目的日志文件。我正在使用Eclipse进行编程。


应用程序路径是什么意思? - Azodious
应用程序路径是我运行源项目的路径。 - Gapchoos
4个回答

4

I would use

String logPath = new File(".").getAbsolutePath();

.. for start.


实际上,我正在将这个值传递到数据库中的一个表格。当我使用这种方法时,该值被带入了数据库。 - Gapchoos
1
好的,恭喜你,但这完全不相关 :) 你可以随意处理那个字符串。例如,你甚至可以发推特。 - Jiri Kremser
尽管我是以这种方式提供路径的,但我仍然无法得到表字段日志路径。这就是我添加属性的方法:propertyInterface.addProperty("LogPath",logPath); 但是,当我硬编码日志路径时,它可以正常工作。 - Gapchoos

4

使用

String AbsolutePath = new File(".").getAbsolutePath();

解释: File(“.”) 表示当前目录,getAbsoultePath() 返回当前目录的绝对路径。
希望这有所帮助 :-)

1
你使用的是哪种编程语言?
Java:
File directory = new File (".");
System.out.println ("Current directory's canonical path: " +     directory.getCanonicalPath());
System.out.println ("Current directory's absolute  path: " + directory.getAbsolutePath());

1
这个调用:new File(".").getAbsolutePath() 可以给你的应用程序提供当前工作目录。希望这回答了你的问题。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接