2014-01-11 140 views
0

以下這幾行幫助我瀏覽文件並將文件內容存儲到myFile變量中?使用JFileChooser選擇文件

另外,有人可以告訴我以下是什麼意思?

JFrame frame = null; 

(System.getProperty("user.dir") 

代碼:

JFrame frame = null; 
    JFileChooser fChoose = new JFileChooser(System.getProperty("user.dir")); 
    int returnVal = fChoose.showOpenDialog(frame); 
    File myFile = fChoose.getSelectedFile(); 
+2

如果您無法自行回答你的問題,你應該閱讀[this](http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html)。 – Qiu

+0

'(System.getProperty(「user.dir」)'這意味着代碼的作者顯然不能很好地理解'JFileChooser',它**默認**到'user.dir'。請注意['JFileChooser'](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html)的Java文檔是開始回答此問題的好地方。 –

+0

是你想存儲的文件內容是否包含文本? – cdMinix

回答

1

如果你想從文件讀取文本,這將是你要走的路:

FileInputStream fis = new FileInputStream(myFile); 
BufferedReader stream = new BufferedReader(new InputStreamReader(fis, "ISO-8859-1")); 
String line; 
while ((line = stream.readLine()) != null) { 
    //save your lines to an array or list  
} 
stream.close(); 
fis.close(); 
1

JFrame frame = null; 

意味着你宣佈一個JFrame變量,並將其分配給null

System.getProperty("user.dir") 

意味着你得到了用戶的工作目錄。

參見:

http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

在您的主要問題,你應該閱讀有關的JFrame和JFileChooser中的一些教程。

http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html

http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

相關問題