java - 使用 InputSource 时发现 FileNotFound

标签 java android xpath

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(filename);' 时,我得到了 File insideString s`。

读取文件:

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

写入文件:

private void writeToFile(String data, String fileName) {
    try {
        String UTF8 = "UTF-8";
        int BUFFER_SIZE = 8192;
        FileOutputStream fileOutputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, UTF8), BUFFER_SIZE);
        bufferedWriter.write(data);
        bufferedWriter.close();
    } catch (IOException e) {
        Log.e("writeToFile: ", "Datei-Erstellung fehlgeschlagen: " + e.toString());
    }
}

那么我必须更改什么才能让 InputSource 找到文件?

最佳答案

readFromFile 中,您正在调用 openFileInput,而不是 FileInputStream 构造函数。所以当你想创建一个 InputSource 时做同样的事情:

inputSource = new InputSource(openFileInput(filename));

关于java - 使用 InputSource 时发现 FileNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31672784/

相关文章:

构建器模式中的 java 类型推断

java - 使用反射,我可以确定 Java static final 字段是否将被内联吗?

android - Realm 会自动下载所有同步的 Realms 吗?

java - 处理两种情况的 XPath 1.0 查询

selenium - 如何使用Selenium Webdriver中的xpath查找包含的文本的确切值?

java - 使用camel dsl接收sqs消息属性?

java - 如何访问 ThreadPoolExecutor 中正在运行的线程?

android - 在 chromebook/chrome 操作系统上注册 Android 应用程序的协议(protocol)处理程序以打开外部链接?

android - 如何保存我的 Activity 状态?

java - XPath 为具有 defaultNamespace 的 xml 返回 null