为什么我必须在函数签名中将BufferedWriter参数定义为Writer?

时间:2014-04-30 03:08:20

标签: java csv

我写了以下方法:

protected void writeCSV(BufferedWriter out, String s) {
    try {
        out.write('"');
        out.write(s.replaceAll("\"", "\"\""));
        out.write('"');
        out.write(',');
    }
    catch (IOException e) {
        JOptionPane.showMessageDialog(window, "IOException");
    }
}

它被称为:

out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath.toString()), "UTF-8"));
writeCSV(out, "CSV cell");

Eclipse不喜欢这样,并要求我将方法签名中第一个参数的类型更改为Writer。我知道BufferedWriter扩展了Writer,但为什么不被认可为BufferedWriter

1 个答案:

答案 0 :(得分:0)

Looser指出了我的答案。在out实例化Writer之前,BufferedWriter被简单地声明为{{1}}。咄...