所以,我只是想再次使用属性文件,但目前我无法加载它们!我已经浪费了 1 小时的工作只是为了让它工作,但不知何故我做不到。我的问题类似于 this one , 但 Java 就是不获取文件!
这是我的代码:
快速打包.ProfileManager;
导入java.io.FileInputStream; 导入 java.util.Properties;
导入android.app.Activity; 导入 android.content.Context; 导入 android.net.wifi.WifiManager; 导入 android.os.Bundle; 导入 android.view.View; 导入 android.view.View.OnClickListener; 导入 android.widget.CheckBox; 导入 android.widget.Toast;
public class PMMain extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String defaultProfileProperties = "defaultProfile.properties";
Properties properties = new Properties();
properties.load(new FileInputStream(defaultProfileProperties));
...
我已经试过在文件名前加一个“/”,但也没用。
这是我的项目目录:
我在“properties.load ...”行收到 IOException
最佳答案
查看 this introduction在 Android 中使用/访问属性文件。
基于该链接,将属性文件放入/assets 文件夹并使用以下代码:
// Read from the /assets directory
try {
InputStream inputStream = assetManager.open("defaultProfile.properties"");
Properties properties = new Properties();
properties.load(inputStream);
System.out.println("The properties are now loaded");
System.out.println("properties: " + properties);
} catch (IOException e) {
System.err.println("Failed to open microlog property file");
e.printStackTrace();
}
关于java - 属性文件不会被识别!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6471131/