-1

ユーザー指定のディレクトリにxmlファイルを置きたいです。そのために、私はすでにファイルエキスパートを作成しました。

      File file = new File("d:\Expert");

これは、d:\ ドライブにファイル export .xml を作成することです。しかし、ここではプログラム自体のパスについて言及しました。しかし、ユーザーはこのパスを認識できませんでした。私がする必要があるのは、ユーザーが出力コンソールのどこにでもパスを指定する必要があることです。このために、変数を args[].in ネット Bean に渡しました。オブジェクトのプロパティを介して引数を指定しました ->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();
           }

}

4

2 に答える 2

1

(質問を正しく理解している場合)ユーザーにを提供しますJFileChooser

于 2011-10-17T03:57:05.443 に答える
1

あなたがこれまでに提供したものに基づいて (これは少し混乱していて読みにくいです)、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);
     // ...
 }

それが望まない場合は、質問をより明確に説明する必要があります。

于 2011-10-17T05:19:26.747 に答える