我一直在bufferedreader中获取错误消息,并在最后捕获catch异常。我已经在这里待了三天了。如果有人可以告诉我我做错了什么,并告诉我如何修复我的代码,那将是非常感激的。

package week07;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFileChooser;


public class Week07 {


    public static void main(String args[]) throws IOException
    {

        {
            JFileChooser chooser = new JFileChooser();
            int result = chooser.showOpenDialog(null);
        //check result
            File file = chooser.getSelectedFile();
        }
        // Use DataInputStream to read binary NOT text.
        BufferedReader br = new BufferedReader(new FileReader(file));

        String strLine;
        List<Double> numbers= new ArrayList<Double>();

        //Read File Line By Line
        while ((strLine = br.readLine()) != null)   {
        // Add number from file to list
        numbers.add( parseDecimal(strLine));
        }
        //Close the input stream
        ((BufferedReader) numbers).close();

        System.out.println(numbers);
    }catch (Exception e){
        e.printStackTrace();
    }


private static Double parseDecimal(String strLine) {
    // TODO Auto-generated method stub
   return null;
}
}

最佳答案

((BufferedReader) numbers).close();


如何将List转换为BufferedReader,然后在其上调用close()。显然,您将获得例外。将其更改为br.close()并最好在finally块中使用null检查`。

10-08 00:27
查看更多