Java使FileReader成为成员变量
所以我经常使用这条线Java使FileReader成为成员变量,java,filereader,Java,Filereader,所以我经常使用这条线 FileReader reader = new FileReader(file) 我想让这一行成为一个成员变量。像这样: private FileReader reader = new FileReader(file); 问题是java不喜欢这样,并给出了错误: The default constructor cannot handle exception type FileNotFoundException thrown by implicit super const
FileReader reader = new FileReader(file)
我想让这一行成为一个成员变量。像这样:
private FileReader reader = new FileReader(file);
问题是java不喜欢这样,并给出了错误:
The default constructor cannot handle exception type FileNotFoundException thrown by implicit super constructor. Must define an explicit constructor Java(16777362)
所以我做了一个这样的构造函数:
protected Json() throws FileNotFoundException
{
//
}
问题是,我在不同的类中使用了JSON类,这些类会给出相同的错误,所以我不得不编辑其他类,但结果看起来非常难看,并带来了很多问题。这个问题有解决办法吗?所以我决定这样做
JSONArray jsonArray = (JSONArray) jsonParser.parse(new FileReader(file));
你考虑过尽快处理(异常)吗?或者这是不可行的?再看一看,或者,所有这些都讨论了相同的问题,使用聚合来代替合成