我应该在哪里放置文本文件以供 netbeans 读取?

rob*_*uin 2 java netbeans

我是java新手,但我一直在寻找答案,但不幸的是还没有找到适合我情况的答案。我想知道将我的文件“icecream.txt”放在哪里以便 netbeans 可以读取它。如果我编写绝对路径,我的程序就可以工作,但我不想这样做,因为它需要在其他学生的计算机上运行而无需更改。我附上了我放置文件的位置的图像。任何帮助,将不胜感激。 整洁的豆子文件夹

如果有帮助的话我的代码如下

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");
   }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

您需要将该文件移动icecream.txt到 netbeans 项目中。例如

IceCreamSales (Project)
  |
  +--icecream.txt
  |
  +--src
      |
      +--icecreamsales
            |
            +--IceCreamSales.java
Run Code Online (Sandbox Code Playgroud)