简体   繁体   English

使用JFileChooser选择文件

[英]Selecting file using JFileChooser

Will these lines below help me to browse to a file and store the file content into the myFile variable? 以下这些行是否可以帮助我浏览文件并将文件内容存储到myFile变量中?

Also, can someone please tell me what the following means? 另外,有人可以告诉我以下是什么意思吗?

JFrame frame = null; 

and

(System.getProperty( "user.dir" )

Code: 码:

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

This 这个

JFrame frame = null;

means you're declaring a JFrame variable and assigning it to null . 表示您声明了一个JFrame变量并将其赋值为null

This 这个

System.getProperty( "user.dir" )

means you're getting the user working directory. 意味着您正在获取用户工作目录。

See also: 也可以看看:

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

On your main question, you should read some tutorial about JFrame and JFileChooser. 在您的主要问题上,您应该阅读一些关于JFrame和JFileChooser的教程。

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

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

If you want to read text from the file, this would be your way to go: 如果您想从文件中读取文本,这将是您的方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM