How to import a Text file content to a JTextArea in a Java application?
how to import a Text file content to a JTextArea in a Java application using JFileCh开发者_如何转开发ooser?
should be something like the following code:
JFileChooser chooser = new JFileChooser();
int returnVal = chooser.showOpenDialog(null); //replace null with your swing container
File file;
if(returnVal == JFileChooser.APPROVE_OPTION)
file = chooser.getSelectedFile();
}
JTextArea text = new JTextArea();
BufferedReader in = new BufferedReader(new FileReader(file));
String line = in.readLine();
while(line != null){
text.append(line + "\n");
line = in.readLine();
}
The basic logic:
BufferedReader in = new BufferedReader(new FileReader(file));
String line = in.readLine();
while(line != null){
text.append(line + "\n");
line = in.readLine();
}
import java.awt.BorderLayout;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;
class DocumentViewer {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
final JFrame f = new JFrame("Document Viewer");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JFileChooser fileChooser = new JFileChooser();
JPanel gui = new JPanel(new BorderLayout());
final JEditorPane document = new JEditorPane();
gui.add(new JScrollPane(document), BorderLayout.CENTER);
JButton open = new JButton("Open");
open.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
int result = fileChooser.showOpenDialog(f);
if (result==JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try {
document.setPage(file.toURI().toURL());
} catch(Exception e) {
e.printStackTrace();
}
}
}
});
gui.add(open, BorderLayout.NORTH);
f.setContentPane(gui);
f.pack();
f.setSize(400,300);
f.setLocationByPlatform(true);
f.setVisible(true);
}
});
}
}
To import the contents of a file into a JTextArea you simply follow these steps!
- Create a frame and add a JTextArea to it.
- You declare and initialize a JFileChooser.
- You add a listener to the JFileChooser.
- In your actionPerformed, you should take the file that was selected and pass it to a method that would read this file(see NB below).
- In that method, you open a file reader and read the contents of the file, line by line. As you do so, you append each line to the JTextArea.
- When you get to the end of the file, you close the file reader.
- Run the program and you should be good to go.
The above steps are good enough to perform your task. However, when you give it a try, i would edit my post and add a possible solution.
NB: You must note that when you select a file with a JFileChooser, it returns an Object of type File. You should then use the getName()
method provided by the File class to get the name of the file.
Links that might be of help!
JFileChooser
File
Java tutorials on how to use the JFileChooser
Determine the filename given from the FileChooser, read the contents of the file into a String (e.g. using a StringBuilder
), set the contents of the JTextArea to the contents of the buffer using JTextField#setText(String)
.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser jf = new JFileChooser();
final JEditorPane document = new JEditorPane();
int returnval=jf.showDialog(this, null);
File file = null;
if(returnval == JFileChooser.APPROVE_OPTION)
file = jf.getSelectedFile();
String str ;
try {
byte bt[]= Files.readAllBytes(file.toPath());
str=new String(bt,"UTF-8");
System.out.println(str);
} catch (IOException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
}
精彩评论