【发布时间】:2018-02-18 05:58:17
【问题描述】:
我有一个代码可以将 String+variable+string 保存到 .properties 文件中 但它将它保存为字符串,当我再次加载它以编程该变量不再是变量时 - 它只是一个字符串。
如何做到这一点 -> 加载字符串+变量+字符串并将其加载到我的 java 代码表单文件 name.properties 中的一个变量中?
String userNickname = api.getClientInfo(movedevent.getClientId()).getNickname();
String Text="[i]Welcome [/i][color=red][b]" + userNickname+ "[/b][/color][i] on channel";
Properties prop = new Properties();
try {
prop.setProperty("Text", Textregister);
File f = new File("server.properties");
if(!f.exists()){
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f, false), "UTF-8")), true);
prop.store(out, "Autoconfig");
}
} catch (Exception io) {
io.printStackTrace();
}
然后将其加载到程序中。
InputStream input = null;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("server.properties"), "UTF8"));
// load a properties file
prop.load(in);
// get the property value and print it out
Text= prop.getProperty("Textregister");
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我得到欢迎 " + userNickname+ " 频道"
但我想在频道上欢迎我的昵称”
有人可以帮忙吗?
【问题讨论】:
-
您能否打印变量 userNickname 的内容来确定变量的实际内容是什么?这必须在设置值之后。
-
问题是当我把它放到 name.properties 然后我读它。我得到了“频道上的欢迎哈肯”,但如果其他人加入同一个频道,他会收到相同的消息“频道上的欢迎哈肯”,因为从 name.properties 加载后用户昵称它也不是一个变量,它是唯一的字符串,如 Welcome...on 频道。跨度>
标签: java string variables bufferedreader