如何找到绝对路径

如何找到绝对路径

问题描述:

大家好,

我已经开发了一个Java应用程序,它将运行一个批处理文件,每次运行此批处理文件时都会创建一个日志文件,我正在通过我的Java程序读取此日志文件,每次都在我所在的文件夹中创建此日志文件.正在运行我的程序,因为我知道程序的位置,所以我在Java程序中硬编码了日志文件的路径以进行读取,现在我想使我的程序通用,这意味着我希望它应该找到自己所在的日志文件并从中读取在那里.

请帮助我解决这个问题.

Hi All,

I have developed a java application which will run a batch file, a log file is getting created everytime this batch file is running, i am reading this log file through my java program, this log file is getting created everytime in the folder from where i am running my program, since i know location of program so i am hardcoding the path of log file in java program to read, now i want to make my program generic means i want it should find itself where is the log file and read it from there.

Please help me in that

您应该阅读相对路径,这样就足够了.

如果确实需要绝对路径,则应使用 File.getAbsolutePath () [ ^ ]
You should read the relative path, that should be enough.

When you really, really need the absolute path you should use File.getAbsolutePath()[^]
File oFile = new File("Filename.txt");
String strAbsolutePath = oFile.getAbsolutePath();