[英]Android, how to load a file as a 'File' object without knowing the file's path
在我的應用程序中,我需要從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的答案
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.