當我運行這個程序時,它找不到指向它的文件。我把這兩個文本文件放到程序的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);
}
}
我有一個當我使用eclipse時,這個問題與此類似。你能檢查以下路徑:路徑currentRelativePath = Paths.get(「」); – michaelp