简体   繁体   English

用Java写入/读取文件

[英]Write/Reading Files in Java

I am working on an assignment and cannot get this method to produce the correct output to the file. 我正在分配作业,无法使用此方法将正确的输出输出到文件。 I am supposed to get the mean and write it to the file. 我应该得到平均值并将其写入文件。 Their is the StatsDemo class and the StatsFile class. 它们是StatsDemo类和StatsFile类。 I am kind of a beginner at Java so I'd like just a little help. 我是Java的初学者,所以我只需要一点帮助。 My method in the StatsFile class is currently like this: 我在StatsFile类中的方法当前是这样的:

//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);
}

The demo is as such: 演示是这样的:

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");
        }
    }
}

The catches and throws still kind of confuse me. 捕捉和抛出仍然使我感到困惑。 My issue is that it currently gives me a question mark instead of the mean when i open the file. 我的问题是,当我打开文件时,它给了我一个问号,而不是平均值。 Any help will be appreciated. 任何帮助将不胜感激。

If an exception that occurs within the try block, control jumps to the catch block. 如果try块中发生异常,则控制跳至catch块。 You should think about what should happen in that case. 您应该考虑在这种情况下会发生什么。 You may be able to correct the problem and continue; 您可能可以纠正问题并继续; you may wish to look at the problem and rethrow and exception, or you may wish just to let your caller handle the problem. 您可能希望查看问题,重新抛出和异常,或者您只希望让呼叫者处理问题。 In the last case, you don't need a catch at all, you can just need a throws on your method declaration. 在最后一种情况下,您根本不需要捕获,只需要对方法声明进行throws

Catching an exception and doing nothing is rarely a good idea as the problem is just being ignored. 捕获异常而不采取任何措施很少是一个好主意,因为问题只是被忽略了。 Remember that code flow will carry on after the catch clause unless another exception is thrown, so if the file does not exist, you will still process the return (accumulator/counter) line, which is not what you want. 请记住,除非抛出另一个异常,否则代码流将在catch子句之后进行,因此,如果文件不存在,您仍将处理return (accumulator/counter) ,这不是您想要的。

Looking at your code, your method already throws a FileNotFoundException, so just remove the try and the catch. 查看您的代码,您的方法已经引发了FileNotFoundException,因此只需删除try和catch。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM