Java Android属性文件
我正在尝试在Android应用程序中加载保存在属性文件中的一些数据 我已将我的属性文件放在src文件夹下。每次我尝试从文件加载数据时,它总是告诉我FileNotFoundException open failed eNot(没有这样的文件或目录) 我的代码如下: 此代码用于保存文件(新建) 此代码用于加载数据Java Android属性文件,java,android,properties,filenotfoundexception,Java,Android,Properties,Filenotfoundexception,我正在尝试在Android应用程序中加载保存在属性文件中的一些数据 我已将我的属性文件放在src文件夹下。每次我尝试从文件加载数据时,它总是告诉我FileNotFoundException open failed eNot(没有这样的文件或目录) 我的代码如下: 此代码用于保存文件(新建) 此代码用于加载数据 properties = new Properties(); InputStream is = null; // First try loading from
properties = new Properties();
InputStream is = null;
// First try loading from the current directory
try {
File f = new File("src/com/example/testphonegap/SilverAngel.properties");
is = new FileInputStream(f);
// Try loading properties from the file (if found)
properties.load(is);
GetPersonaliseSettings();
GetUserSettings();
GetFavSettings();
}
catch ( Exception e ) {
is = null;
}
你能告诉我我做错了什么吗?是保存文件的地方还是我的代码中缺少了什么?试试这个
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("com/example/testphonegap/SilverAngel.properties");
properties.load(inputStream);
确保不要在属性文件名之前添加“src”。将其放入资产文件夹并从那里加载
AssetManager assetManager = context.getAssets();
InputStream inputStream = assetManager.open("SilverAngel.properties");
properties.load(inputStream);
这是一个Android应用程序,因此文件存储在Android设备上,而不是您的计算机上。 如果要将文件保存在SD卡上,可以编写以下命令:
String root = Environment.getExternalStorageDirectory().toString();
File dir = new File(root + "/subfolderForYourApp");
dir.mkdirs();
这将在SD卡上为您的应用程序创建一个子文件夹。
要在此目录中创建新文件,请执行以下操作:
File file = new File(dir, "SilverAngel.properties");
FileOutputStream fileOut = new FileOutputStream(file);
properties.store(fileOut, "Properties");
fileOut.close();
不要忘记将权限添加到清单:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
您是否尝试过将目录更改为外部位置,如外部存储器中的新文件夹中的位置否。。我如何才能做到这一点请File f=new File(getApplicationContext().getFilesDir()+“/YourFolderName”);将该文件放在projectDirectory/res/raw中,而不是src中。感谢您的帮助。感激
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />