我想在用户指定的目录中有一个 xml 文件。为此,我已经创建了一个文件专家。
File file = new File("d:\Expert");
这是在 d:\ 驱动器创建文件导出 .xml。但在这里我已经提到了程序本身的路径。但用户无法识别此路径。我需要做的是用户应该在他的输出控制台中指定他想要的路径。为此,我在 args[].in net beans 中传递了变量,我通过对象的属性给出了参数 ->run->arguments->d:... 稍后在我编写的程序中,如下代码所示。这给了我输出。但文件没有在 d:... 创建它只是附加字符串。我如何创建文件用户指定的目录???谁能提供我的代码片段???
public class New {
void Expor() throws IOException, TransformerConfigurationException
//adding a node after the last child node of the specified node.
Element child = doc.createElement("body");
root.appendChild(child);
System.out.println("file created successfully");
//TransformerFactory instance is used to create Transformer objects.
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = sw.toString();
File file = new File("expert");// creates the file if i mentioned D:/Export.xml
//System.out.println(file.getName());
// System.out.println(file.getAbsolutePath());
String path=readpath+filename;
System.out.println(path);
BufferedWriter bw = new BufferedWriter
(new OutputStreamWriter(new FileOutputStream(file)));
bw.write(xmlString);
bw.flush();
bw.close();
}
public static void main(String argv[]) throws SQLException, IOException,
{
if (argv.length == 0) {
System.out.println("No Command Line arguments");
} else {
System.out.println("You provided " + argv.length
+ " arguments");
for (int i = 0; i < argv.length; i++) {
System.out.println("args[" + i + "]: "
+ argv[i]);
}
}
New e= new New ();
e.connectDB();
}
}