简体   繁体   中英

How to access a text file from a java EXPORTED project eclipse

I have a small program where it reads data from a text file. I am using Eclipse Java SE 1.6. The program is reading a file and displaying the results on a JLabel.

I am using private Scanner x; to go read my text file. Here is the function itself below:

public void readYearSetting()
{
    try {
        x = new Scanner(new File("yearSetting"));
    }
    catch(Exception e) {
        System.out.println("Could not find file");
    }
    while(x.hasNext()) {
        String year = x.next();
        yearSetting = Integer.parseInt(year);
        displayingInsightsYearLabel.setText("Displaying " + year);
    }

}

I made a text file named yearSetting . The file itself does not have the extension of .txt which doesn't matter as it still opens it. I am working on a Macbook and have download Eclipse for my mac. (In case that helps).

The file is located within the main root folder. Which is no problem at all as it reads the files. When I click on the button "Run" from inside Eclipse, it runs smooth and great. So it works.

However, Now that i'm finished with the program, I would like to export it to be a standalone program the user can install and have. In order to do so, I know I must export as a JAR file. I have followed the steps on how to achieve that and I export it. But, it won't play the program. It crashes. Also calls a Null Pointer Exception error. And where I call for the method of readYearSetting , I comment it out to test it and then I export it as a JAR file and it works! So apparently it's not reading the files. I don't know why and how, if when I run it through eclipse it works fine.

I have looked and looked and no luck in finding an answer. I have moved the file to any where in the project you can think of, messed with the code and googled it but still no luck. So my question is, How would I get the program to read the file after the program is exported from eclipse as a JAR file. Am I missing something? Am I not suppose to use bufferedReader? Why is it not detecting the file? What's the correct way of reading a text file even after exporting your project as a JAR?

First thing you pass only file name as yearSetting Change the file Name like and then try yearSetting.txt . and Check Full path of file Using Print Statement . Demo Code to Read File Using Scanner :

 File file = new File("yearSetting.txt");
 try {
 Scanner scanner = new Scanner(file);
 while (scanner.hasNextLine()) {
  String line = scanner.nextLine();
  System.out.println(line);
  }
  scanner.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }`:

Happy to Help

Apparently, the text files are not like the images that are export along with the project. The Text files must be within the same place where the Java JAR file is exported to in order to read them. The text files will not be compiled inside the JAR file but rather physically outside where the .exe or JAR file program is located.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM