从java程序中删除硬编码的文件路径

时间:2021-11-20 20:46:51

i have created a simple java program in which i am creating a text file and reading the data written inside it. The problem is i don't want to hardcode the path of the file because after developing the application i have created a installer package for my program which allow users to install it on there systems. Now the problem is like end user can install the file anywhere (i.e. in there C , D or E drive) and in those cases i am getting FileNotFoundException Exception.

我创建了一个简单的java程序,其中我正在创建一个文本文件并读取其中写入的数据。问题是,我不想硬编码文件的路径,因为在开发应用程序之后,我为我的程序创建了一个安装包,允许用户在那里的系统上安装它。现在的问题是,最终用户可以在任何地方安装文件(例如在C、D或E驱动器中),在这些情况下,我将获得FileNotFoundException异常。

My code-- This is the code which i am using to create and write some text to the text file.

我的代码——这是我用来创建和写入文本文件的代码。

    FileWriter file = new FileWriter("E:\\TextFile.txt",true);
    BufferedWriter writer = new BufferedWriter(file);
    writer.write(input);
    write.newLine();
    write.close();

This is the code which i am using to read text from the text file.

这是我用来从文本文件中读取文本的代码。

    FileReader read = new FileReader("E:\\TextFile.txt");
    BufferedReader data = new BufferedReader(read);

I have one more file for which i hardcoded the path of the file.

我还有一个文件,我硬编码了文件的路径。

    System.setProperty("webdriver.chrome.driver","D:\\New Folder\\chromedriver.exe");

As you can see in all my code i hardcoded the paths("E:\TextFile.txt","E:\TextFile.txt" and "D:\New Folder\chromedriver.exe"). Is there any way in java to remove them. I went through the similar questions but was not able to figure out how to detect the location of the file.

正如您在我的所有代码中看到的,我硬编码了路径(“E:\TextFile.txt”,“E:\TextFile”。txt”和“D:\新文件夹\ chromedriver.exe”)。在java中有什么方法可以删除它们吗?我也遇到过类似的问题,但是我不知道如何检测文件的位置。

3 个解决方案

#1


2  

I made the changes as per the suggetions and it worked for me-

我按照建议做了修改,这对我很有效

// This give me the path of the application where it is installed
String Path = new File("").getAbsolutePath();

Then i add the file name along with the path to get the file.

然后添加文件名以及获取文件的路径。

// Here i am adding the name of the file to the path to read it 
FileReader  read = new FileReader(Path+"\\TextFile.txt"); 

// Here i am adding the name of the file to the path to write it 
FileWriter file = new FileWriter(Path+"\\TextFile.txt",true);

#2


0  

You can store the file location in a properties file and then read the file location from that into a variable at runtime. Your installer would have to store the file location in the properties file as part of its installation process.

可以将文件位置存储在属性文件中,然后在运行时将文件位置读入变量中。安装程序必须将文件位置存储在属性文件中,作为安装过程的一部分。

You could also have the file stored in the applications classpath and use relative pathnames to get to it.

您还可以将文件存储在应用程序类路径中,并使用相对路径名来访问它。

#3


0  

I don't think this the answer to what you are asking, but It's a solution to your problem. What you are using there is an absolute path, meaning you specify the whole URL of the file, you can instead use relative paths, which are relative to the location of your application, just like you have .class files in your JAR, you can have a folder for your files and it will be always at the same location relative to the program location.

我不认为这是你的问题的答案,但这是你问题的解决方案。你使用绝对路径,这意味着你指定整个文件的URL,您可以使用相对路径,而是相对于您的应用程序的位置,就像你在JAR . class文件,你可以有一个你的文件和文件夹将总是在相同的位置相对于程序的位置。

So instead of this:

而不是:

FileReader read = new FileReader("E:\\TextFile.txt");

You can have this:

你可以这样:

FileReader read = new FileReader("..\MyFiles\TextFile.txt");

Or something like this.

或者是这样的。

#1


2  

I made the changes as per the suggetions and it worked for me-

我按照建议做了修改,这对我很有效

// This give me the path of the application where it is installed
String Path = new File("").getAbsolutePath();

Then i add the file name along with the path to get the file.

然后添加文件名以及获取文件的路径。

// Here i am adding the name of the file to the path to read it 
FileReader  read = new FileReader(Path+"\\TextFile.txt"); 

// Here i am adding the name of the file to the path to write it 
FileWriter file = new FileWriter(Path+"\\TextFile.txt",true);

#2


0  

You can store the file location in a properties file and then read the file location from that into a variable at runtime. Your installer would have to store the file location in the properties file as part of its installation process.

可以将文件位置存储在属性文件中,然后在运行时将文件位置读入变量中。安装程序必须将文件位置存储在属性文件中,作为安装过程的一部分。

You could also have the file stored in the applications classpath and use relative pathnames to get to it.

您还可以将文件存储在应用程序类路径中,并使用相对路径名来访问它。

#3


0  

I don't think this the answer to what you are asking, but It's a solution to your problem. What you are using there is an absolute path, meaning you specify the whole URL of the file, you can instead use relative paths, which are relative to the location of your application, just like you have .class files in your JAR, you can have a folder for your files and it will be always at the same location relative to the program location.

我不认为这是你的问题的答案,但这是你问题的解决方案。你使用绝对路径,这意味着你指定整个文件的URL,您可以使用相对路径,而是相对于您的应用程序的位置,就像你在JAR . class文件,你可以有一个你的文件和文件夹将总是在相同的位置相对于程序的位置。

So instead of this:

而不是:

FileReader read = new FileReader("E:\\TextFile.txt");

You can have this:

你可以这样:

FileReader read = new FileReader("..\MyFiles\TextFile.txt");

Or something like this.

或者是这样的。