在Android中保存文件与文件名空间 [英] Save file in Android with spaces in file name
问题描述
我需要为我的Android应用程序到一个XML文件保存到与含有空格的文件名外部缓存。
I need for my android application to save an xml file into the external cache with a file name which contains space.
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(activity
.getExternalCacheDir().getAbsolutePath()
+ "/Local storage.xml"));
transformer.transform(source, result);
当我手动浏览到我的文件目录,我找到这个文件:本地%20storage.xml
When I browse manually into my file directory I find this file : "Local%20storage.xml".
所以,以后,当我试图用读
So after when I try to read it with
File localStorageXmlFile = new File(activity.getExternalCacheDir()
.getAbsolutePath()
+ "/Local storage.xml");
不过我有个FileNotFoundException异常,因为文件本地storage.xml不能我的设备上找到。
But I have a FileNotFoundException because the file "Local storage.xml" can't be found on my device.
任何想法解决这个?
SEB
Any ideas to solve this? Seb
推荐答案
这是很难确定这个问题的来源,但它来自StreamResult它通过20%替换文件的名称空间。没有针对此问题一个bug报告这里。
It was hard to identify the source of this problem but it comes from StreamResult which replaces spaces in file name by %20. There is a bug report for this issue here.
这是解决方案:
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File(activity
.getExternalCacheDir().getAbsolutePath()
+ "/" + "Local storage" + ".xml"));
Result fileResult = new StreamResult(fos);
transformer.transform(source, fileResult);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fos != null) {
fos.close();
}
}
再次感谢你们俩为试图解决我的问题。
Thanks again to both of you for trying to solve my issue.
这篇关于在Android中保存文件与文件名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!