Android,如何在不知道文件路径的情况下将文件加载为'File'对象



在我的应用程序中,我需要从xml文件中读取元素的内容。

所以我在我的 LocalRead.java 类中以这种方式编写了方法"getValueOfElement":

[...]
    public String getValueOfElement (String filename, String what){
    try{
        File xmlDocument = new File ("/Unknow_Path/"+filename);
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document document = documentBuilder.parse(xmlDocument);
        String lookingFor = document.getElementsByTagName(what).item(0).getTextContent();
        return lookingFor;

    } catch (FileNotFoundException e) {
        System.err.println("----------------- File not found -----------------");
        e.printStackTrace();
        return "";
    } catch (ParserConfigurationException e) {
        System.err.println("----------------- Error creating DocumentBuilder -----------------");
        e.printStackTrace();
        return "";
    } catch (SAXException e) {
        System.err.println("----------------- Error creating the document(Sax) -----------------");
        e.printStackTrace();
        return "";
    } catch (IOException e) {
        System.err.println("----------------- Error creating the document(IO) -----------------");
        e.printStackTrace();
        return "";
    }
}
 [...]

如您所见,当我创建文件"xmlDocument"时,我不知道我的xml文件所在的路径。我使用这个类来创建文件。

import android.content.Context;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileBuilder {
    private String xmlContent;
    private String filename;
    private Context context;
    private FileOutputStream outputStream;
    public FileBuilder(Context context){
        this.context = context;
    }
    public boolean createUserFile(String username, String password){
        this.xmlContent = "<?xml version='1.0' encoding='UTF-8'?>n" +
                "<giocatore>n" +
                "<username>"+username+"</username>n" +
                "<password>"+password+"</password>n" +
                "</giocatore>";
        this.filename = "[D&D]User.xml";
        try{
            outputStream = context.openFileOutput(filename, context.MODE_PRIVATE);
            outputStream.write(xmlContent.getBytes());
            outputStream.close();
            System.out.println("----------------- File created -----------------");
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}

我怎样才能知道路径是什么?

就像CommonsWare在评论中所说的那样,Document可以解析InputStream。因此,您可以使用openFileInput()将文件作为Document加载。这里是完整的代码:

[...]    
public String getValueOfElement (String filename, String what){
            try{
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                Document document = documentBuilder.parse(context.openFileInput(filename));
                String lookingFor = document.getElementsByTagName(what).item(0).getTextContent();
                return lookingFor;

            } catch (FileNotFoundException e) {
                System.err.println("----------------- File not found -----------------");
                e.printStackTrace();
                return "";
            } catch (ParserConfigurationException e) {
                System.err.println("----------------- Error creating DocumentBuilder -----------------");
                e.printStackTrace();
                return "";
            } catch (SAXException e) {
                System.err.println("----------------- Error creating the document(Sax) -----------------");
                e.printStackTrace();
                return "";
            } catch (IOException e) {
                System.err.println("----------------- Error creating the document(IO) -----------------");
                e.printStackTrace();
                return "";
            }
        }
[...]

请记住,openFileInput()需要一个Context

感谢@CommonsWare的回答

最新更新