이 프로그램을 실행할 때 직접 지시 한 파일을 찾을 수 없습니다. 두 개의 텍스트 파일을 프로그램의 src 폴더에 넣었습니다.이 파일을 File f = new File("filename.txt")
이라고 부르면됩니다. 하지만 그건 효과가 없습니다. 나는 또한 File()
의 정확한 디렉토리를 사용해 보았지만 어느 것도 작동하지 않는다. 파일에는 이름과 그 옆에있는 금액이 들어 있습니다. 어떤 아이디어?.txt 파일을 찾을 수없는 이유는 무엇입니까?
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);
}
}
이클립스를 사용했을 때 실제로 이것과 비슷한 문제. 다음을 사용하여 파일 경로를 확인할 수 있습니까? Path currentRelativePath = Paths.get (""); – michaelp