在用户指定的路径中创建文件
我想在用户指定的目录中有一个xml文件。为此我已经创建了一个文件专家。
File file = new File("d:\Expert");
这是在 d:\ 驱动器中创建文件导出 .xml。但在这里我已经提到了程序本身的路径。但用户无法识别该路径。我需要做的是用户应该在输出控制台中指定他想要的路径。为此,我在 args[] 中传递了变量。在 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();
}
}
i want to have a xml file in the user specifed directory. for that i hav already created a file expert .
File file = new File("d:\Expert");
this is create the file export .xml at d:\ drive. but here i have mentioned the path in the program itself. but user couldn't recognize this path. what i need to do is user should specify the path wherever he wants in his output console. for this i passed the variable in args[].in net beans i gave the arguments through properties of object ->run->arguments->d:...
later in the program i wrote like below code. and this is giving me the output.
but file is not creating at d:...it simply appends the strings. how do i create a file user specied directory???can anyone provide me the code snippet???
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您到目前为止提供的内容(有点混乱,并且难以阅读),您似乎想要进行 2 项更改:
1:将您的 main 方法更改为例如:
2:将您的 xmlExport 方法更改为:
如果这不是您想要的,那么您需要更清楚地解释您的问题。
Based on what you've provided so far (which is a bit of a mess, and hard to read), it looks like you want to make 2 changes:
1: Change your main method to be something like:
2: Change your xmlExport method to be:
If that's not what you want, then you'll need to explain your question more clearly.
(如果我正确理解问题)为用户提供 <代码>JFileChooser。
(If I understand the question correctly) offer the user a
JFileChooser
.