2011-10-17 35 views
-1

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

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

这是在d:\ drive创建文件导出.xml文件。但在这里我已经提到了程序本身的路径。但用户无法识别此路径。我需要做的是用户应该在他的输出控制台中指定他想要的路径。为此,我在args []中传递了变量,在net beans中,我通过对象的属性 - >运行 - >参数 - > 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(); 
      } 

}

+3

你可以加你整个班吗?你的代码现在难以辨认。 –

回答

1

基于到目前为止您所提供的内容(这是一个有点乱,而且很难看),它看起来像你想改变2:

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

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

1

(如果我理解正确的问题),为用户提供了一个JFileChooser

相关问题