0

When I run this program, it cannot find the files I direct it to. I put the two text files into the src folder of the program, and to my understanding all I would have to do to call it is File f = new File("filename.txt"). But that doesn't work. I also tried using the exact directory inside of File() but it doesn't work either. The files just contain a name and an amount of money beside them. Any ideas?

import java.io.*;
import java.util.Scanner;

class Donors {
File donor2;
File donor3;
Scanner inD2;
Scanner inD3;

Donors(File d2, File d3){
    donor2 = d2;
    donor3 = d3;
}

double totalDonations(){
    double total = 0;
    try{
    inD2 = new Scanner(donor2);
    while(inD2.hasNext()){
        total += inD2.nextDouble();
    }
    }catch(java.io.FileNotFoundException e){
        System.out.println("File can't be found");
    }

    try{
    inD3 = new Scanner(donor3);
    while(inD3.hasNext()){
        total += inD3.nextDouble();
    }
    }catch(java.io.FileNotFoundException e){
        System.out.println("File can't be found");
    }

    return total;
}

public void closeFile(){
    inD2.close();
    inD3.close();
  }
}

public class DonorCalculations {
 public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int userInput;
    File donor2 = new File("H:\\CSC 191\\Assignment9\\src\\resources\\donor2.txt");
    File donor3 = new File("donor3.txt");
    Donors dObj = new Donors(donor2, donor3);

    do{
        System.out.println("SELECT");
        System.out.println("1. Total money from donations");
        System.out.println("2. Total donation from a individual");
        System.out.println("0. Quit");

        userInput = input.nextInt();
        System.out.println();

        switch(userInput){
            case 1:
                System.out.println(dObj.totalDonations());
                break;
            case 2:
                System.out.println("Enter donor's name: ");
                String name = input.next();
                //dObj.donorTotal(name);
                break;
            case 0:
                System.out.println("Goodbye!");
                break;
        }
        System.out.println();
    }while(userInput != 0);
  }
}
ToonLink
  • 235
  • 1
  • 2
  • 9
  • I had a problem really similar to this when I was using eclipse. Can you check the filepath with the following: Path currentRelativePath = Paths.get(""); – michaelp Apr 30 '14 at 00:30

2 Answers2

7

You've now embedded the files into your application making them embedded resources. You can no longer access them as if they were files.

Instead you need to use the resource lookup functionality Java provides, for example...

InputStream donor2 = getClass().getResourceAsStream("/resources/donor2.txt");

This may or may not be a good thing.

If you must read the contents from flat files, then those files need to be located within a relative location of the execution context of the program.

You can determine the execution context of your current program by using System.out.println(System.getProperty("user.dir"));, which will print the current directory that the program is executing within. Your text files should be located within a relative context of this directory while you're developing.

When it's built, the files will need to be within the same relative context as the program is been executed from

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Change:

File donor2 = new File("H:\\CSC 191\\Assignment9\\src\\resources\\donor2.txt");
File donor3 = new File("donor3.txt");
Donors dObj = new Donors(donor2, donor3);

To:

File donor2 = new File("H:\\CSC 191\\Assignment9\\src\\resources\\donor2.txt");
File donor3 = new File(getClass().getClassLoader().getResource("donor3.txt"));
Donors dObj = new Donors(donor2, donor3);
Petro
  • 3,484
  • 3
  • 32
  • 59