默认构造函数无法处理隐式超级构造函数抛出的异常类型 IOException。必须定义显式构造函数

2024-01-27

我有两个java类Configuration.java和login.java

配置.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);
            }

        }
    }
}

登录.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";

谁能帮我解决这个错误


   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 ...

   }  

还可以像这样更改您的配置文件:

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

        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

默认构造函数无法处理隐式超级构造函数抛出的异常类型 IOException。必须定义显式构造函数 的相关文章

随机推荐