我想在用戶指定的目錄中有一個xml文件。爲此我已經創建了一個文件專家。在用戶指定的路徑中創建文件
File file = new File("d:\Expert");
這是在d:\ drive創建文件導出.xml文件。但在這裏我已經提到了程序本身的路徑。但用戶無法識別此路徑。我需要做的是用戶應該在他的輸出控制檯中指定他想要的路徑。爲此,我在args []中傳遞了變量,在net beans中,我通過對象的屬性 - >運行 - >參數 - > 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();
}
}
你可以加你整個班嗎?你的代碼現在難以辨認。 –