我有一个试图从“concerts.txt”文件中读取信息的程序。我把它放在我的文件夹的根文件中,即使我100%肯定文件存在,它仍然返回“错误:找不到文件”异常。我已经尝试将它从源文件夹移动到根项目文件夹,以及放入文件的整个地址(“C:...”),似乎没有任何工作。我继续得到错误信息,似乎无论我做什么。此外,我尝试将try catch块移动到包含构造函数ConcertEvent的类中,所做的就是获取在ConcertEvent的所有三个实例中编写的数据的第一个实例。我不确定如何将其更改为获取正确数据的位置(concert1的名称应该是Maroon 5,concert2的名称应该是One Direction但是现在两个实例的名称都显示为Maroon 5)请帮助我!
另外,我附上了我应该从
读取的构造函数和.txt文件public class ConcertDriver
{
private static String name = null;
private static char orderType = ' ';
private static int numTicketsOrdered = 0;
private static String bandName = null;
private static double ticketPrice = 0;
private static int showCapacity = 0;
private static double amountOwed = 0;
private static boolean answer = true;
private static int response = 0;
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
try
{
Scanner input = new Scanner(new File("concerts.txt"));
ConcertEvent concert1 = new ConcertEvent(input);
ConcertEvent concert2 = new ConcertEvent(input);
ConcertEvent concert3 = new ConcertEvent(input);
System.out.println("Redbird Concert Hall");
System.out.println();
System.out.println("Please choose your concert:");
System.out.println("1. " + concert1.getBandName());
System.out.println("2. " + concert2.getBandName());
System.out.println("3. " + concert3.getBandName());
System.out.println("4. Quit");
System.out.println();
response = keyboard.nextInt();
while(answer)
{
switch(response)
{
case 1:
{
manageSales(concert1);
}
case 2:
{
manageSales(concert2);
}
case 3:
{
manageSales(concert3);
}
case 4:
{
answer = false;
FileWriter output = new FileWriter(new File("report.txt"));
output.write(concert1.getBandName() + "\n");
output.write(concert1.getShowCapacity() + "\n");
output.write(concert1.getTotalTicketsSold() + "\n");
output.write(concert2.getBandName() + "\n");
output.write(concert2.getShowCapacity() + "\n");
output.write(concert2.getTotalTicketsSold() + "\n");
output.write(concert3.getBandName() + "\n");
output.write(concert3.getShowCapacity() + "\n");
output.write(concert3.getTotalTicketsSold() + "\n");
}
default:
{
System.out.println("Invalid option, please try again.");
}
}
}
}
catch(Exception e)
{
System.out.println("Error: File Not Found");
}
}
/*
* Handles sales, and uses other private helper methods to confirm ticket amounts and calculate necessary data
*/
private static void manageSales(ConcertEvent concert)
{
bandName = concert.getBandName();
showCapacity = concert.getShowCapacity();
ticketPrice = concert.getTicketPrice();
System.out.println("There are " + showCapacity + " tickets remaining for " + bandName + ".");
System.out.println();
System.out.println("Enter your first and last name:");
name = keyboard.nextLine();
System.out.println();
System.out.println("How is your order placed? B for box office or P for phone");
orderType = keyboard.next().toUpperCase().charAt(0);
ticketPrice = concert.getTicketPrice();
if(orderType == 'P')
{
ticketPrice += 4;
}
System.out.println();
System.out.println("How many tickets would you like?");
numTicketsOrdered = keyboard.nextInt();
confirmAmount(numTicketsOrdered, concert);
}
/*
* Checks to make sure that there is enough tickets available for sale
*/
private static void confirmAmount(int numTickets, ConcertEvent concert)
{
if(numTickets > showCapacity)
{
System.out.println("You asked for " + numTicketsOrdered + " to "
+ concert.getBandName() + ", but there are only "
+ concert.getShowCapacity() + " tickets remaining.");
}
else
{
amountOwed = ConcertCalculator.calculate(numTicketsOrdered, concert.getTicketPrice());
printReceipt(bandName, name, numTicketsOrdered, amountOwed);
}
}
/*
* Prints receipt for purchase
*/
private static void printReceipt(String bandName, String name, int numTickets, double amountOwed)
{
System.out.println("Concert: " + bandName);
System.out.println("Name: " + name);
System.out.println("Number tickets: " + numTickets);
System.out.println("Amount owed: " + amountOwed);
}
}
public ConcertEvent(Scanner input)
{
try
{
input = new Scanner(new File("concerts.txt"));
bandName = input.nextLine();
showCapacity = input.nextInt();
ticketPrice = input.nextDouble();
input.nextLine();
}
catch(Exception e)
{
System.out.println("Error: file not found");
}
Maroon 5
15
40
One Direction
10
50
Pearl Jam
20
30
答案 0 :(得分:0)
此:
Scanner input = new Scanner(new File("concerts.txt"));
将在用户目录中查找该文件,而不是在根目录中查找。您可以通过在代码中的某处放置System.out.println(System.getProperty("user.dir"));
来找到用户的目录。如果要在特定位置获取文件,可能需要使用文件的完整路径。
或者,通过Class的getResourceAsStream(...)
方法将这些资源作为资源通常更好。如果使用此方法,资源的路径(此处为文本文件)将相对于类文件的路径。
答案 1 :(得分:0)
Here您可以看到如何使用类路径查看相对路径。
如果您知道文件的相对位置,那么您可以轻松地访问项目的根目录,并从那里到达您的文件。