Printwriter FileNotFoundException 异常
Printwriter FileNotFoundException
我正在尝试编写一个英里跟踪器程序来跟踪用户步行、跑步或游泳的距离。该程序要求用户输入会话期间步行的距离,将其存储在双精度中,从文件中读取有关先前步行总距离的数据,将最近会话的距离添加到过去的总距离以创建新的总距离,并将该总数写入存储它以供将来使用的文件。
编写代码时,当我尝试将它们指向文件时,IDE 在打印编写器和扫描仪上显示错误:
Scanner reader = new Scanner(storage); //error at second Scanner
PrintWriter writer = new PrintWriter(storage); // error at second PrintWriter
错误显示为 "FileNotFoundException"
当放在 try 块中时,错误消失,程序在 运行:
时打印 catch 块错误报告
catch(FileNotFoundException e){
System.out.println("Check that the text file is in the correct directory.");
e.printStackTrace();
}
这是我使用 PrintWriter 编写的第一个程序,因此我将不胜感激一些关于我做错了什么的指示和解释。
完整代码如下:
import java.io.File;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.util.Scanner;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.String;
public class Main {
public static void main(String[] args) {
//gets interval data from user
System.out.println("Please type the amount of miles walked.");
Scanner input = new Scanner(System.in);
double inMiles = input.nextDouble();
//creates file and scanner that reads to file
File storage = new File ("Mile Tracker//Mile.txt");
try {
storage.createNewFile();
}
catch(IOException e ){
System.out.println("File not created.");
}
try {
Scanner reader = new Scanner(storage);
//takes data from file and sets it to a variable
double Miles = reader.nextDouble();
double TotalMiles = Miles + inMiles;
//PrintWriter reader = new PrintWriter( file );
//PrintWriter that clears the file
/*PrintWriter clearwriter = new PrintWriter(storage);
clearwriter.print("");
clearwriter.close();
*/
PrintWriter writer = new PrintWriter(storage);
writer.print(TotalMiles);
writer.close();
}
catch(FileNotFoundException e){
System.out.println("Check that the text file is in the correct directory.");
e.printStackTrace();
}
}
}
如果您已经有一个名为 "Mile Tracker" 的目录,请检查它是否与 java class 在同一路径中,因为您没有明确给出 File
构造函数。如果你没有"Mile Tracker"目录,那么你修改代码为:
File storage = new File ("Mile Tracker//Mile.txt");
try {
storage.getParentFile().mkdirs();
storage.createNewFile();
}
catch(Exception e ){
System.out.println("File not created.");
}
至此,FileNotFound
错误应该得到解决。
我正在尝试编写一个英里跟踪器程序来跟踪用户步行、跑步或游泳的距离。该程序要求用户输入会话期间步行的距离,将其存储在双精度中,从文件中读取有关先前步行总距离的数据,将最近会话的距离添加到过去的总距离以创建新的总距离,并将该总数写入存储它以供将来使用的文件。 编写代码时,当我尝试将它们指向文件时,IDE 在打印编写器和扫描仪上显示错误:
Scanner reader = new Scanner(storage); //error at second Scanner
PrintWriter writer = new PrintWriter(storage); // error at second PrintWriter
错误显示为 "FileNotFoundException"
当放在 try 块中时,错误消失,程序在 运行:
时打印 catch 块错误报告catch(FileNotFoundException e){
System.out.println("Check that the text file is in the correct directory.");
e.printStackTrace();
}
这是我使用 PrintWriter 编写的第一个程序,因此我将不胜感激一些关于我做错了什么的指示和解释。
完整代码如下:
import java.io.File;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.util.Scanner;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.lang.String;
public class Main {
public static void main(String[] args) {
//gets interval data from user
System.out.println("Please type the amount of miles walked.");
Scanner input = new Scanner(System.in);
double inMiles = input.nextDouble();
//creates file and scanner that reads to file
File storage = new File ("Mile Tracker//Mile.txt");
try {
storage.createNewFile();
}
catch(IOException e ){
System.out.println("File not created.");
}
try {
Scanner reader = new Scanner(storage);
//takes data from file and sets it to a variable
double Miles = reader.nextDouble();
double TotalMiles = Miles + inMiles;
//PrintWriter reader = new PrintWriter( file );
//PrintWriter that clears the file
/*PrintWriter clearwriter = new PrintWriter(storage);
clearwriter.print("");
clearwriter.close();
*/
PrintWriter writer = new PrintWriter(storage);
writer.print(TotalMiles);
writer.close();
}
catch(FileNotFoundException e){
System.out.println("Check that the text file is in the correct directory.");
e.printStackTrace();
}
}
}
如果您已经有一个名为 "Mile Tracker" 的目录,请检查它是否与 java class 在同一路径中,因为您没有明确给出 File
构造函数。如果你没有"Mile Tracker"目录,那么你修改代码为:
File storage = new File ("Mile Tracker//Mile.txt");
try {
storage.getParentFile().mkdirs();
storage.createNewFile();
}
catch(Exception e ){
System.out.println("File not created.");
}
至此,FileNotFound
错误应该得到解决。