最近需要读取一个XML文件到FILE文件中,简单几步搞定
- 将XML文件放在assets文件夹下面
- 然后新建一个FILE文件,
String defaultPath = mContext.getFilesDir().getAbsolutePath() + “/defaultGoodInfo”;
File file = new File(defaultPath); - try{ writeBytesToFile(mContext.getAssets().open(“add_rospec.xml”),file);
}catch (IOException E){
Log.i(“xin”,“writeBytesToFile ex==”+E);
} - 其中
public static void writeBytesToFile(InputStream is, File file) throws IOException{
FileOutputStream fos = null;
try {
byte[] data = new byte[2048];
int nbread = 0;
fos = new FileOutputStream(file);
while((nbread=is.read(data))>-1){
fos.write(data,0,nbread);
}
}
catch (Exception ex) {
Log.e(“xin”,“write file Exception=”+ex);
}
finally{
if (fos!=null){
fos.close();
}
}
}