Write/Reading 个文件在 Java
Write/Reading Files in Java
我正在做作业,无法使用此方法生成正确的文件输出。我应该得到平均值并将其写入文件。它们是 StatsDemo class 和 StatsFile class。我是 Java 的初学者,所以我需要一点帮助。我在 StatsFile class 中的方法目前是这样的:
//returns the calculated arithmetic average
public double calculateMean(String filename) throws FileNotFoundException
{
// declare variables step 5
double accumulator = 0.0;
int counter =0;
String line;
try{
File input = new File(filename);
//create a Scanner object passing it the File object
Scanner keyboard = new Scanner(input);
//for (int i = 0; i < input.length(); i++){
// Read a double from the file.
while(keyboard.hasNextDouble()){
accumulator += keyboard.nextDouble();
// Add to counter
counter++;
}
keyboard.close();
}catch(FileNotFoundException e){
}
return (accumulator/counter);
}
演示是这样的:
import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.*;
public class StatsDemo {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
DecimalFormat threeDec = new DecimalFormat("0.000");
Scanner keyboard = new Scanner(System.in);
String filename; // the user input file name
System.out.print("Enter the file name: ");
filename = keyboard.nextLine();
FileStats fileObj = new FileStats(filename);
try{
PrintWriter name = new PrintWriter("Results.txt");
name.println("mean = " + threeDec.format(fileObj.getMean()));
name.println("Standard Deviation = " + threeDec.format(fileObj.getStdDev()));
name.close();
}catch(IOException e){
System.out.println("Error");
}
}
}
接球和投球仍然让我感到困惑。我的问题是,当我打开文件时,它目前给我一个问号而不是平均值。任何帮助将不胜感激。
如果在 try 块内发生异常,控制将跳转到 catch 块。您应该考虑在这种情况下应该发生什么。您也许可以更正问题并继续;您可能希望查看问题并重新抛出异常,或者您可能希望让调用者处理问题。在最后一种情况下,您根本不需要 catch,您只需在方法声明中使用 throws
即可。
捕获异常而不做任何事情很少是一个好主意,因为问题只是被忽略了。请记住,除非抛出另一个异常,否则代码流将在 catch 子句之后继续,因此如果文件不存在,您仍将处理 return (accumulator/counter)
行,这不是您想要的。
查看您的代码,您的方法已经抛出 FileNotFoundException,因此只需删除 try 和 catch。
我正在做作业,无法使用此方法生成正确的文件输出。我应该得到平均值并将其写入文件。它们是 StatsDemo class 和 StatsFile class。我是 Java 的初学者,所以我需要一点帮助。我在 StatsFile class 中的方法目前是这样的:
//returns the calculated arithmetic average
public double calculateMean(String filename) throws FileNotFoundException
{
// declare variables step 5
double accumulator = 0.0;
int counter =0;
String line;
try{
File input = new File(filename);
//create a Scanner object passing it the File object
Scanner keyboard = new Scanner(input);
//for (int i = 0; i < input.length(); i++){
// Read a double from the file.
while(keyboard.hasNextDouble()){
accumulator += keyboard.nextDouble();
// Add to counter
counter++;
}
keyboard.close();
}catch(FileNotFoundException e){
}
return (accumulator/counter);
}
演示是这样的:
import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.*;
public class StatsDemo {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
DecimalFormat threeDec = new DecimalFormat("0.000");
Scanner keyboard = new Scanner(System.in);
String filename; // the user input file name
System.out.print("Enter the file name: ");
filename = keyboard.nextLine();
FileStats fileObj = new FileStats(filename);
try{
PrintWriter name = new PrintWriter("Results.txt");
name.println("mean = " + threeDec.format(fileObj.getMean()));
name.println("Standard Deviation = " + threeDec.format(fileObj.getStdDev()));
name.close();
}catch(IOException e){
System.out.println("Error");
}
}
}
接球和投球仍然让我感到困惑。我的问题是,当我打开文件时,它目前给我一个问号而不是平均值。任何帮助将不胜感激。
如果在 try 块内发生异常,控制将跳转到 catch 块。您应该考虑在这种情况下应该发生什么。您也许可以更正问题并继续;您可能希望查看问题并重新抛出异常,或者您可能希望让调用者处理问题。在最后一种情况下,您根本不需要 catch,您只需在方法声明中使用 throws
即可。
捕获异常而不做任何事情很少是一个好主意,因为问题只是被忽略了。请记住,除非抛出另一个异常,否则代码流将在 catch 子句之后继续,因此如果文件不存在,您仍将处理 return (accumulator/counter)
行,这不是您想要的。
查看您的代码,您的方法已经抛出 FileNotFoundException,因此只需删除 try 和 catch。