2013-11-28 16 views
0

我想將另一個類中的選定文件插入其他類中,方法doit()中的變量in在​​中。 我怎樣才能插入方法度特(),在類元素,而不是此文件「d:\ Probe.txt」,將選中的文件插入FileReader

public void doit() { 
    try { 
     in = new BufferedReader(new FileReader("D:\\Probe.txt")); 
     out = new StreamResult("D:\\data.xml"); 
     initXML(); 
     String str; 
     while ((str = in.readLine()) != null) { 
      process(str); 
     } 
     in.close(); 
     closeXML(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

值得注意的是這個文件,在從類對話框patch變量, '

if (cmd.equals("Quelldatei auswählen")) { 
    JFileChooser fileChooser = new JFileChooser(); 
    fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 

    int ret = fileChooser.showDialog(this, "auswählen"); 
    if (ret == JFileChooser.APPROVE_OPTION) { 

    File patch = fileChooser.getSelectedFile(); 
    contentPane.add(new JLabel("Quelldatei ist: " + patch)); 
} 

回答

0

我明白你的問題,你想在你的doit()方法中的文件,而不使用此方法內的BufferedReader?

如果即時通訊的權利,試試這個:

將文件保存在一個文件對象,並給這個對象作爲參數傳遞給喜歡你的DOIT()方法:

public void doit(File file){ 
    ... 
} 
+0

謝謝你的回答,但它必須使用BufferedReader – user2994149

+0

oh..then試試這個:'public void doit(String path){in = new BufferedReader(new FileReader(path));}' –

+0

你明白了嗎,我想分配一個選定的文件在來自Element類的「in」變量中,從Dialog類到FileReader的「patch」變量 – user2994149