简体   繁体   中英

Where should I place a text file for netbeans to read?

I am new to java but I have looked for an answer but unfortunately haven't found one specific to my situation. I'd like to know where to place my file "icecream.txt" so that netbeans can read it. My programs works if I code the absolute the path but I'd prefer not to do that as it needs to run on the other student's computers without changes. I have attached an image of where I have placed my file. Any help would be appreciated. neatbeans file folders

My code is bellow if that helps

package icecreamsales;
/**
 *
 * @author anonymous
 */
public class IceCreamSales {

/**
 * @param args the command line arguments
 */
    public static void main(String[] args) {

        try {
             TextIO.readFile("icecream.txt");
        }
        catch (IllegalArgumentException e) {
            System.out.println("Can't open file \"icecream.txt\" for reading!");
            System.out.println("Please make sure the file is present before");
            System.out.println("running the program.");
            System.exit(1);  // Terminates the program.
    }

        int totalIceCreamSales = 0;
        int strawberryIceCreamSales = 0;

        while (!TextIO.eof()) {
            String readLines = TextIO.getln();
            totalIceCreamSales++;

            if (readLines.equals("Strawberry")) {
                 strawberryIceCreamSales++;
            }
        }
        System.out.println("Icecream cone sales totalled " + totalIceCreamSales);
        System.out.println("Strawberry icecream sales totalled " + strawberryIceCreamSales);
        System.out.println("Strawberry icecream is " + ((double) strawberryIceCreamSales/totalIceCreamSales*100) + "%%" + " of total sales");
   }
}

You need to move the file icecream.txt into the netbeans project. For eg

IceCreamSales (Project)
  |
  +--icecream.txt
  |
  +--src
      |
      +--icecreamsales
            |
            +--IceCreamSales.java

Just as @Stultuske said, you can place your text file anywhere. You can even dedicate a folder for future text files. You just have to know it's specific directory / location so that the "program" will know where to find those files needed.

Look at this answer for file directories references

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