git
使用InputSource
时出现错误。
XPath
我不知道为什么,但在使用InputSource inputSource = null;
try {
inputSource = new InputSource(new FileInputStream(filename));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String s = readFromFile(filename);
String uuid = taskItems.get(position).get("uuid");
XPath xPath = XPathFactory.newInstance().newXPath();
try {
Node taskNode = (Node) xPath.evaluate("//task[@uuid='" + uuid + "']", inputSource, XPathConstants.NODE);
Document document = taskNode.getOwnerDocument();
//Füge neue Zeile ein
Node noteNode = document.createElement("task_note");
noteNode.setTextContent(taskItems.get(position).get("task_note"));
taskNode.appendChild(noteNode);
//Speichere Datei
Source input = new DOMSource(document);
Result output = new StreamResult(new File(filename));
TransformerFactory.newInstance().newTransformer().transform(input, output);
} catch (XPathExpressionException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
String s`时。
readFromFile:
String s = readFromFile(filename);', I get the File inside
将writeToFile:
private String readFromFile(String fileName) {
String ret = "";
String UTF8 = "UTF-8";
int BUFFER_SIZE = 8192;
try {
InputStream inputStream = openFileInput(fileName);
if (inputStream != null) {
BufferedReader bufferedReader1 = new BufferedReader(new InputStreamReader(inputStream, UTF8), BUFFER_SIZE);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ((receiveString = bufferedReader1.readLine()) != null) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
} catch (FileNotFoundException e) {
Log.e("readFromFile: ", "Datei nicht gefunden: " + e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.e("readFromFile: ", "Kann Datei nicht lesen: " + e.toString());
e.printStackTrace();
}
return ret;
}
那么我需要更改什么才能让InputSource找到文件?
答案 0 :(得分:3)
在readFromFile
中,您正在调用openFileInput
,而不是FileInputStream
构造函数。因此,当您想要创建InputSource
:
inputSource = new InputSource(openFileInput(filename));