Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中保存文件,文件名中使用空格_Java_Android_Filenotfoundexception - Fatal编程技术网

Java 在Android中保存文件,文件名中使用空格

Java 在Android中保存文件,文件名中使用空格,java,android,filenotfoundexception,Java,Android,Filenotfoundexception,我需要我的android应用程序将一个xml文件保存到外部缓存中,文件名中包含空格 DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(activity .getExternalCacheDir().getAbsolutePath() + "/Local storage.xml")); tran

我需要我的android应用程序将一个xml文件保存到外部缓存中,文件名中包含空格

DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(activity
                    .getExternalCacheDir().getAbsolutePath()
                    + "/Local storage.xml"));

transformer.transform(source, result);
当我手动浏览到我的文件目录时,我发现这个文件:“Local%20storage.xml”

所以当我试着读它的时候

File localStorageXmlFile = new File(activity.getExternalCacheDir()
                .getAbsolutePath()
                + "/Local storage.xml");
但是我有一个FileNotFoundException,因为在我的设备上找不到文件“Local storage.xml”

有什么办法解决这个问题吗?
Seb

尝试使用基本URL编码:

File localStorageXmlFile = new File(activity.getExternalCacheDir()
                .getAbsolutePath()
                + "/Local%20storage.xml");
此管理帮助您:

此外,在使用文件时,请确保您在操作系统中使用了正确的文件分隔符(在您的情况下,硬编码/应该可以工作,因为Android是基于linux的,但这只是为了确保。 这可能是另一种选择:

File localStorageXmlFile = new File(activity.getExternalCacheDir()
                .getAbsolutePath() + File.separator 
                + "Local storage.xml");

最后一种选择是尝试替换空格。尝试将“”替换为“\”

很难确定此问题的根源,但它来自StreamResult,它将文件名中的空格替换为%20。此问题有错误报告

以下是解决方案:

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();
    }
}

再次感谢你们两位试图解决我的问题。

这对我来说很有用,只是多了一行代码

String fileName = "name with spaces" + ".xml";                  
fileName.replace(" ", "\\ ");

您不应该自己生成文件名,而是使用
新文件(activity.getExternalCacheDir(),“Local storage.xml”)
-它可以帮你解决问题。不知道是什么原因导致了你的问题。谢谢你的回答,但这并不能解决我的问题。在我发布这个问题之前,我尝试了你的解决方案Erwald,我没有例外,但它没有达到我的预期。事实上,它创建了一个名为“Local%20storage.xml”而不是“Local storage.xml”的xml文件。你的第二个答案也不起作用,而且你不能在java中逃逸像“\”这样的空格。谢谢你的答案。你在那里救了我一天。我和你一样解决了这个问题。前斜杠还是后斜杠?很抱歉,后斜杠我会更正的。希望它对你有用。