io ho fatto così:
codice:
private File xmlFile = null;
public Xml() {
String pathJar = this.getClass().getProtectionDomain().getCodeSource().getLocation().toString();
int s = pathJar.lastIndexOf("/");
String end = pathJar.substring(0, s + 1);
xmlFile = new File(end + "todo.xml");
}
public void create() throws ParserConfigurationException, TransformerConfigurationException, TransformerException, IOException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element todos = doc.createElement("todos");
doc.appendChild(todos);
Element todo = doc.createElement("todo");
todo.appendChild(doc.createTextNode("hello"));
todos.appendChild(todo);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
if (!xmlFile.exists()) {
xmlFile.createNewFile();
StreamResult result = new StreamResult(xmlFile);
transformer.transform(source, result);
}
}
il path sembra giusto:
file:/media/MATTE/Project/JAVA/ToDoList/build/classes/
però poi mi ridà l'errore no such file or directory.