Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在用户指定的路径中创建文件_Java_File_Netbeans_Io_Args - Fatal编程技术网

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

Java 在用户指定的路径中创建文件,java,file,netbeans,io,args,Java,File,Netbeans,Io,Args,我希望在用户指定的目录中有一个xml文件。为此,我已经创建了一个文件专家 File file = new File("d:\Expert"); 这是在d:\drive创建文件export.xml。但在这里,我提到了程序本身的路径。但用户无法识别此路径。我需要做的是,用户应该在其输出控制台中的任何位置指定路径。为此,我在args[]中传递了变量。在netbean中,我通过object->run->arguments->d:.的属性给出了参数。。。 后来在程序中,我编写了如下代码。这是

我希望在用户指定的目录中有一个xml文件。为此,我已经创建了一个文件专家

      File file = new File("d:\Expert");
这是在d:\drive创建文件export.xml。但在这里,我提到了程序本身的路径。但用户无法识别此路径。我需要做的是,用户应该在其输出控制台中的任何位置指定路径。为此,我在args[]中传递了变量。在netbean中,我通过object->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();
           }

}

如果我正确理解了问题,请向用户提供一个答案。

根据您目前提供的有点混乱且难以阅读的内容,看起来您需要进行两项更改:

1:将您的主要方法更改为:

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);
     // ...
 }