Вопрос

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();
           }

}

Это было полезно?

Решение

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.

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top