简体   繁体   English

浏览要读取的文本文件

[英]Browse For a Text File to Read From

I'm trying to make a program in Java where the user is able to click a browse button and then be able to browse through directories for a text file. 我正在尝试用Java编写程序,使用户能够单击浏览按钮,然后能够在目录中浏览文本文件。

Once they select that text file, I plan to read from it in order to load some of its data into variables, but the part im working on right now is just getting the browse button to work; 一旦他们选择了该文本文件,我打算从中读取文本文件,以便将其中的一些数据加载到变量中,但是我目前正在研究的部分只是使浏览按钮起作用。 then I will move onto the reading from the text file. 然后我将继续阅读文本文件中的内容。

Right now I have a JButton with an empty action listener, and a non-editable text field in which i want to load the files path into. 现在,我有一个JButton其中包含一个空的动作侦听器,以及一个我要在其中加载文件路径的不可编辑文本字段。

I see others talking about the JFileChooser class, but the examples they are always using the JFileChooser to for saving files or actually opening them, I do not want the file to be opened for the user, I just want the path so I know where to read from. 我看到其他人在谈论JFileChooser类,但是他们始终使用JFileChooser来保存文件或实际打开它们的示例,我不想为用户打开文件,我只想要路径,所以我知道在哪里阅读。 Let me know if you need more information. 如果您需要更多信息,请与我们联系。

Thank you for all the help, I was able to find the information I needed with in the JavaDocs, I now have my absolute path loaded into the JTextField and from here I will move onto the reading of the file, Thanks all. 谢谢您的所有帮助,我能够在JavaDocs中找到所需的信息,现在我将绝对路径加载到JTextField中,从这里我将继续阅读文件,谢谢。

browseButton.addActionListener( new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        // yet to come...
    }
});

JFileChooser does not actually open the file. JFileChooser实际上不会打开文件。 What it returns is the path to the file(s) chosen. 它返回的是所选文件的路径。 JFileChooser is definitely the way to go, based on your description of the issue. 根据对问题的描述,绝对可以使用JFileChooser It allows the user to browse to a file, whose path is then returned back to you, and from there you can do whatever you need with the file. 它允许用户浏览到文件,然后将其路径返回给您,从那里您可以对文件进行任何所需的操作。

So, the actionPerformed(ActionEvent e) method on the browseButton object is where you would trigger the appearance of the JFileChooser . 因此,您将触发JFileChooser的外观的browseButton对象上的actionPerformed(ActionEvent e)方法。 You also attach an ActionListener to the JFileChooser , to react to the event that closes the dialog. 您还可以将ActionListener附加到JFileChooser ,以对关闭对话框的事件做出反应。 In the actionPerformed method, attached to the ActionListener that is listening to the JFileChooser is where you will be able to get at the value selected by the user (ie if it was a file chosen, a folder, or if they canceled the dialog without selecting anything). actionPerformed方法中,附加到正在侦听JFileChooserActionListener ,您将可以在该位置获得用户选择的值(即,如果选择的是文件,文件夹,还是他们取消了对话框而没有选择任何东西)。

The official tutorial by Sun provides examples (and example code) for both saving and opening. Sun官方教程提供了保存和打开的示例(和示例代码)。

Also, the JFileChooser does not read or open the file! 另外, JFileChooser不会读取或打开文件! It only provides a user-frontend to search for the file to open/save and then returns a File -object which you can then use to read from the file (with a BufferedReader (for plain text) for example). 它仅提供了一个用户前端来搜索要打开/保存的File ,然后返回一个File ,您可以使用该对象从文件中读取(例如,使用BufferedReader (用于纯文本))。

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

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