我读到了有关属性和资源包的内容.但我无法区分这些.何时使用Properties文件以及何时使用Resource bundle.
要加载属性文件,请使用以下代码
Properties tempProp = new Properties();
FileInputStream propsFile = new FileInputStream(xyz.properties);
tempProp.load(propsFile);
Run Code Online (Sandbox Code Playgroud)
加载资源包
ResourceBundle labels =
ResourceBundle.getBundle("xyz", currentLocale);
Enumeration bundleKeys = labels.getKeys();
Run Code Online (Sandbox Code Playgroud)
在这两种情况下(在资源包和Properites中)我们都使用属性文件.我发现的一个区别是,为了存储我们使用属性文件的应用程序特定数据,并使用i18n数据,我们使用资源包.我不知道我是对的.
我想知道上面两个的用法.这两者有什么区别.
我想在我的Java代码中序列化一些对象.我不想把它放在硬盘上的一些随机文件夹中.我希望它在我的eclipse项目文件夹中的A FOLDER中.如何制作此文件夹并将对象存储在其中?
这是一个好习惯吗?如果我试图从这个项目中创建一个独立的JAR,会不会有问题?
使用 Maven,是否可以覆盖 Java 常量?
想象我有
public static final String buildBy="Eclipse";
Run Code Online (Sandbox Code Playgroud)
使用Maven时应改为
public static final String buildBy="Maven";
Run Code Online (Sandbox Code Playgroud)
那可能吗?谢谢 :-)
我已经尝试过在属性文件的路径上进行变体,但似乎无法使其正确。
Here's the structure:
src/
??? properties.properties
??? teln
??? ConnectMUD.java
??? IOUtil.java
??? PropertiesReader.java
Run Code Online (Sandbox Code Playgroud)
和错误:
run:
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at teln.PropertiesReader.getProps(PropertiesReader.java:16)
at teln.ConnectMUD.main(ConnectMUD.java:18)
Run Code Online (Sandbox Code Playgroud)
和相关课程:
package teln;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PropertiesReader {
private static final Logger LOG = Logger.getLogger(PropertiesReader.class.getName());
private static Properties props = new Properties();
public static Properties getProps() {
try {
props.load(PropertiesReader.class.getResourceAsStream("/teln/teln.properties"));
} catch (IOException ex) {
Logger.getLogger(PropertiesReader.class.getName()).log(Level.SEVERE, null, ex);
}
LOG.fine(props.toString());
return …Run Code Online (Sandbox Code Playgroud) 我有24个非常冗长的字符串,例如"输入基因符号或通用名称......"现在,通常使用较短的字符串常量和较小的属性,我在类中声明内部枚举.但是,由于这些字符串中有24个,我不确定是否有更有效或更清晰的方式来组织我的代码.
附加信息:
这是我目前的代码:
private static String[] getTemplateDefaults()
{
String[] defaults = new String[]
{
"Enter Gene symbol or common name",
"Enter blah blah blah",
"Enter blah blah blah",
"Enter blah blah blah",
"Enter blah blah blah",
// continued for 19 more times...
};
return defaults;
}
Run Code Online (Sandbox Code Playgroud)
真的只是寻找一些想法来组织它,而不是看起来如此繁琐.
谢谢!
我正在为学校做一个像Cleverbot这样的自动聊天客户端.我有一切工作,但我需要一种方法来建立响应的知识库.我打算制作一个矩阵,其中包含我需要机器人的所有响应,但我认为每次我想要添加对机器人的响应时都很难编辑代码.这是我对知识库矩阵的代码:
`String[][] Database={
{"hi","hello","howdy","hey"},//possible user input
{"hi","hello","hey"},//response
{"how are you", "how r u", "how r you", "how are u"},
{"good","doing well"}`
Run Code Online (Sandbox Code Playgroud)
我如何从文本文件中制作这样的矩阵?有没有比从文本文件中读取更好的方法来处理这个问题?
我在使用InputStream和类Loader函数时遇到空指针异常,但是在使用FileInputStream时,它正在正确读取属性文件。
为什么我收到此错误?下面是我的代码。
public String readProperties()
{
String result = "";
Properties prop = new Properties();
String file = "test.properties";
//InputStream fins = getClass().getClassLoader().getResourceAsStream(file);
try
{
prop.load(new FileInputStream(file));
//prop.load(fins);
}
catch (IOException e) {
e.printStackTrace();
}
String nation = prop.getProperty("Nation");
String city = prop.getProperty("City");
String state = prop.getProperty("State");
result = "I live in "+city+" in "+state+" in "+nation;
return result;
}
Run Code Online (Sandbox Code Playgroud)