<;标识符>;应为Java语言
我正在尝试从类中的属性文件加载数据。但是我得到了错误<;标识符>;应为Java语言,java,Java,我正在尝试从类中的属性文件加载数据。但是我得到了错误预期值。我没有得到我在这方面做错了什么。请建议我做错了什么 public class ToDoList { private Properties prop = new Properties(); { prop.load(this.getClass().getClassLoader().getResourceAsStream("file.properties"));//Here i am getting the erro
预期值
。我没有得到我在这方面做错了什么。请建议我做错了什么
public class ToDoList {
private Properties prop = new Properties();
{
prop.load(this.getClass().getClassLoader().getResourceAsStream("file.properties"));//Here i am getting the error
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
错误就像
<identifier> expected
illegal start of type
')' expected
';' expected
illegal start of type
<identifier> expected
';' expected
invalid method declaration; return type required
';' expected
应为
类型的非法开始
")"预期"
';' 预期
类型的非法开始
预期
';' 预期
方法声明无效;需要返回类型
';' 预期
您忘记了try
关键字。应该是
try {
// statements
} catch (IOException ioe) {
// handle exception
}
不要忘记将代码放入方法。您忘记了
try
关键字。应该是
try {
// statements
} catch (IOException ioe) {
// handle exception
}
不要忘记将代码放入方法中。您使用了初始值设定项块
{…}
,但它不能有catch块,也不能尝试
在该级别,只允许字段、构造函数、类和方法声明
我的解决方案,把所有的都放在一个构造函数中
public ToDoList() {
try {
prop.load(this.getClass().getClassLoader().getResourceAsStream("file.properties"));
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
您使用了初始值设定项块
{…}
,但它不能有catch块,也不能尝试
在该级别,只允许字段、构造函数、类和方法声明
我的解决方案,把所有的都放在一个构造函数中
public ToDoList() {
try {
prop.load(this.getClass().getClassLoader().getResourceAsStream("file.properties"));
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
我假设您的属性文件如下所示:
someName1=someValue1
someName2=someValue2
someName3=someValue3
在我的例子中,属性文件位于项目的根文件夹中
但这是没有必要的
public class Application
{
public static void main(String[] args)
{
Properties prop = new Properties();
try
{
InputStream input = new FileInputStream("./test.properties");
prop.load(input);
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println(prop.get("someName1"));
System.out.println(prop.get("someName2"));
System.out.println(prop.get("someName3"));
}
}
这将显示我在属性文件中设置的值
请注意,您没有在代码中调用任何函数:
public class ToDoList {
//Here you declare your properties
private Properties prop = new Properties();
{// this is not a function
prop.load(this.getClass().getClassLoader().getResourceAsStream("file.properties"));//Here i am getting the error
}
catch (IOException ioe) // you dont start a try-clause
{
ioe.printStackTrace();
}
}
将其更改为:
public class ToDoList {
private Properties prop = new Properties();
public ToDoList(){ //Constructor of the class
try{
// declare the inputstream with that file
InputStream input = new FileInputStream("file.properties");
prop.load(input);
}
catch (IOException ioe)
{
// Error handling code here
ioe.printStackTrace();
}
}
}
我假设您的属性文件如下所示:
someName1=someValue1
someName2=someValue2
someName3=someValue3
在我的例子中,属性文件位于项目的根文件夹中
但这是没有必要的
public class Application
{
public static void main(String[] args)
{
Properties prop = new Properties();
try
{
InputStream input = new FileInputStream("./test.properties");
prop.load(input);
}
catch (IOException e)
{
e.printStackTrace();
}
System.out.println(prop.get("someName1"));
System.out.println(prop.get("someName2"));
System.out.println(prop.get("someName3"));
}
}
这将显示我在属性文件中设置的值
请注意,您没有在代码中调用任何函数:
public class ToDoList {
//Here you declare your properties
private Properties prop = new Properties();
{// this is not a function
prop.load(this.getClass().getClassLoader().getResourceAsStream("file.properties"));//Here i am getting the error
}
catch (IOException ioe) // you dont start a try-clause
{
ioe.printStackTrace();
}
}
将其更改为:
public class ToDoList {
private Properties prop = new Properties();
public ToDoList(){ //Constructor of the class
try{
// declare the inputstream with that file
InputStream input = new FileInputStream("file.properties");
prop.load(input);
}
catch (IOException ioe)
{
// Error handling code here
ioe.printStackTrace();
}
}
}
什么版本的Java?你是有意使用匿名内部类吗?@Seth我在类中有方法,但我没有发布。对不起,我编辑了我的评论,你是在尝试使用匿名内部类吗?什么版本的Java?你是有意使用匿名内部类吗?@Seth我在类中有方法,但我没有发布。对不起,我编辑了我的评论,你是在尝试使用匿名内部类吗?Java不是一种过程语言,所以如果你需要执行你的
load
语句,它需要放在一个可调用的块(如构造函数、方法或类初始化块)中。Java不是一种过程语言,因此如果您需要执行像load
语句这样的语句,它需要放在一个可调用的块(如构造函数、方法或类初始化块)中。