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

时间:2018-03-22 22:20:51

标签: java android

在我的应用程序中,我需要从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;
        }

    }

}

我怎样才能找出路径是什么?

1 个答案:

答案 0 :(得分:0)

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

[...]    
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的回答