java 默认构造函数无法处理隐式超级构造函数抛出的异常类型 IOException。必须定义一个显式构造函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28854207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Default constructor cannot handle exception type IOException thrown by implicit super constructor. Must define an explicit constructor
提问by faith66
I have two java classes Configuration.java and login.java
我有两个 java 类 Configuration.java 和 login.java
Configuration.java
配置文件
public class Configuration {
public Configuration() {}
public String getparams() throws IOException {
Properties properties = new Properties();
FileInputStream fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");
try {
properties.load(fileStream);
String ip = (String) properties.get("IP");
String port = (String) properties.get("Port");
return ip + port;
} finally {
try {
fileStream.close();
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
}
login.java
登录.java
...
...
Configuration cfg=new Configuration();
String ip=cfg.getparams();// error
String port=cfg.getparams();//error
private final String LOGIN_URL = "http://"+ ip +":"+port+"/webservice/login.php";
can anyone help me please to resolve the error
谁能帮我解决这个错误
采纳答案by Rick Sanchez
public class Login extends Activity implements OnClickListener {
Configuration cfg;
String ip;
String port;
private String LOGIN_URL;
//other variables
protected void onCreate(Bundle savedInstanceState) {
//here you initialize your variables
try {
cfg = new Configuration();
String[] params = cfg.getparams();
ip = params[0];
port = params[1];
LOGIN_URL = "http://"+ ip +":"+port+"/webservice/login.php";
} catch (IOException e) {
//handle the exception
}
//.. your other code ...
}
//... your other methods ...
}
Also change your Configuration file like this:
还要像这样更改您的配置文件:
public class Configuration {
public Configuration() {}
public String[] getparams() throws IOException {
Properties properties = new Properties();
FileInputStream fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");
try {
properties.load(fileStream);
String ip = (String) properties.get("IP");
String port = (String) properties.get("Port");
return new String[]{ip, port};
} finally {
try {
fileStream.close();
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
}
回答by sushantsha
Why don't you change the signature of getParam to not throw an error. I see you are doing it partially for call to properties.load();
为什么不更改 getParam 的签名以不抛出错误。我看到你这样做部分是为了调用 properties.load();
Something like:
就像是:
public class Configuration {
公共类配置{
public Configuration() {}
public String getparams() {
Properties properties = new Properties();
FileInputStream fileStream = null;
try {
fileStream = new FileInputStream("C:/.../Desktop/configuration.txt");
properties.load(fileStream);
String ip = (String) properties.get("IP");
String port = (String) properties.get("Port");
return ip + port;
}
catch(IOException ex)
{
return null;
}
finally {
try {
fileStream.close();
} catch (IOException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
You might want to move new Properties into the try block if it can also throw.
如果它也可以抛出,您可能希望将新属性移动到 try 块中。