简体   繁体   中英

Android, how to load a file as a 'File' object without knowing the file's path

In my app I need to read the content of an element from an xml file.

So I write in my LocalRead.java class the method "getValueOfElement" in this way:

[...]
    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 "";
    }
}

 [...]

As you can see, when I create the File "xmlDocument" I don't know the path where my xml file is. I used this class to create the file.

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

    }

}

How can I find out what the path is?

Like CommonsWare says in the comments, Document can parse an InputStream . So you can load the file as a Document using openFileInput() . Here the complete code:

[...]    
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 "";
            }
        }
[...]

Remember that openFileInput() need a Context .

Thanks to @CommonsWare for the answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM