2015-07-28 20 views
1

XPath使用InputSource時出現錯誤。FileNotFound使用InputSource時

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);', I get the File inside字符串s`時。

readFromFile:

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

將writeToFile:

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

回答

3

readFromFile你打電話給openFileInput,而不是FileInputStream的構造函數。所以當你想創建一個InputSource

inputSource = new InputSource(openFileInput(filename)); 
+0

太容易了!謝謝! – korunos

+0

我可以使用我的writeToFile方法來創建新文件,還是必須使用TransformerFactory? – korunos

+1

@korunos:使用'new StreamResult(openFileOutput(filename,Context.MODE_PRIVATE))' –