在用户指定的路径中创建文件

发布于 2024-12-10 10:01:22 字数 2052 浏览 0 评论 0原文

我想在用户指定的目录中有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨落成白 2024-12-17 10:01:22

根据您到目前为止提供的内容(有点混乱,并且难以阅读),您似乎想要进行 2 项更改:

1:将您的 main 方法更改为例如:

public static void main(String argv[]) throws Exception 
{
    New e= new   New ();
    e.connectDB();
    if(argv.length == 0)
        e.xmlExport("D:\\export.xml");
    else 
        e.xmlExport(argv[0]);
}

2:将您的 xmlExport 方法更改为:

void xmlExport(String fileName) throws IOException, TransformerConfigurationException
{
     // ...
     File file = new File(fileName);
     // ...
 }

如果这不是您想要的,那么您需要更清楚地解释您的问题。

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:

public static void main(String argv[]) throws Exception 
{
    New e= new   New ();
    e.connectDB();
    if(argv.length == 0)
        e.xmlExport("D:\\export.xml");
    else 
        e.xmlExport(argv[0]);
}

2: Change your xmlExport method to be:

void xmlExport(String fileName) throws IOException, TransformerConfigurationException
{
     // ...
     File file = new File(fileName);
     // ...
 }

If that's not what you want, then you'll need to explain your question more clearly.

你爱我像她 2024-12-17 10:01:22

(如果我正确理解问题)为用户提供 <代码>JFileChooser

(If I understand the question correctly) offer the user a JFileChooser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文