共計(jì) 755 個(gè)字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
在 Java 中,可以使用 java.util.Properties
類來讀取 .properties
文件的內(nèi)容。以下是一個(gè)示例代碼:
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ReadPropertiesFile {public static void main(String[] args) {Properties properties = new Properties();
try {
// 通過文件輸入流加載 properties 文件
FileInputStream file = new FileInputStream("config.properties");
// 加載 properties 文件內(nèi)容到 Properties 對象
properties.load(file);
// 關(guān)閉文件輸入流
file.close();
// 讀取 properties 文件中的屬性值
String username = properties.getProperty("username");
String password = properties.getProperty("password");
System.out.println("Username:" + username);
System.out.println("Password:" + password);
} catch (IOException e) {e.printStackTrace();
}
}
}
請將上述代碼中的 config.properties
替換為你要讀取的 .properties
文件的路徑。然后,你可以通過 getProperty
方法來讀取文件中的屬性值。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完