一 在SD卡创建并读写文件
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>申请权限
File newxmlfile = newFile(Environment.getExternalStorageDirectory(),"aaa.xml");
try{
if(!newxmlfile.exists())
newxmlfile.createNewFile();
}catch(IOException e){
Log.e("IOException", "exception in createNewFile() method");
return;
}
//we have to bind the new file with a FileOutputStream
FileOutputStream fileos =null;
try{
fileos = new FileOutputStream(newxmlfile);
}catch(FileNotFoundExceptione){
Log.e("FileNotFoundException", "can't createFileOutputStream");
return;
}
二
FileOutputStream fileos =null;
try{
fileos=context.openFileOutput("aaa.xml",Context.MODE_WORLD_READABLE);
}catch(FileNotFoundException e){
Log.e("FileNotFoundException", "can't createFileOutputStream");
return;
}
FileInputStream fis=null;
try{
fis=context.openFileInput("aaa.xml");
}catch(FileNotFoundException e){
Log.e("FileNotFoundException", "Couldn't find or open policyfile");
return;
}
方法二(普通方法)
FilepolicyFile = new File("/data/aaa.xml");
FileInputStream fis=null;
try {
fis = new FileInputStream(policyFile);
} catch (FileNotFoundException e) {
Log.w(TAG, "Couldn't find or open policy file " +policyFile);
return null;
}