【问题标题】:Getting Error Message When Attempting to Read .properties file using Java Properties.load Method [duplicate]尝试使用 Java Properties.load 方法读取 .properties 文件时出现错误消息 [重复]
【发布时间】:2021-08-03 20:28:31
【问题描述】:
java
    Properties prop = new Properties();
    FileInputStream fis = new FileInputStream("/Student Management System/Resources/dbconfig.properties");
    prop.load(fis);

我想让程序使用 Java.util.properties 读取 .properties 文件,但它在“prop.load(fis)”方法上给了我以下错误消息:

"Multiple markers at this line
    - Syntax error, insert "SimpleName" to complete 
     QualifiedName
    - Syntax error, insert ")" to complete MethodDeclaration
    - Syntax error, insert "Identifier (" to complete 
     MethodHeaderName
    - Syntax error on token ".", @ expected after this token"

【问题讨论】:

  • 错误信息与代码不匹配。发布完整的方法可能吗?
  • 这些行是否放置在方法中? (询问是因为将声明和语句直接放在类中是初学者的常见错误,这对于声明来说似乎很好,但在第一个语句处中断。)

标签: java syntax-error fileinputstream


【解决方案1】:

错误:

public class MyClass {
    Properties prop = new Properties();
    FileInputStream fis = new FileInputStream("/Student Management System/Resources/dbconfig.properties");
    prop.load(fis);
}

在我的 Eclipse 中,我收到了以下错误消息:

Multiple markers at this line
  - Syntax error, insert ")" to complete MethodDeclaration
  - Syntax error, insert "SimpleName" to complete QualifiedName
  - Syntax error, insert "Identifier (" to complete MethodHeaderName
  - Syntax error on token ".", @ expected after this token

正确:

public class MyClass {
    
    public static void foo() throws IOException {
        Properties prop = new Properties();
        FileInputStream fis = new FileInputStream("/Student Management System/Resources/dbconfig.properties");
        prop.load(fis);
    }

}

prop.load(fis); 是一个声明。语句只能出现在方法(包括构造函数)和初始化块中。

根据您的设计,您可能希望将声明和语句放在您的 main 方法 (public static void main(String... args)) 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-27
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 2019-07-31
    相关资源
    最近更新 更多