使用InputSource时FileNotFound

时间:2015-07-28 09:47:42

标签: java android xpath

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找到文件?

1 个答案:

答案 0 :(得分:3)

readFromFile中,您正在调用openFileInput,而不是FileInputStream构造函数。因此,当您想要创建InputSource

时,请执行相同的操作
inputSource = new InputSource(openFileInput(filename));