关于java读取文本到TextArea的问题
我有一个txt文件,查找与输入对应的行,如何操作?建立一个Label输入框和JTextArea输出区域,代码如何操作?谢谢!...
我有一个txt文件,查找与输入对应的行,如何操作?建立一个Label输入框和JTextArea输出区域,代码如何操作?谢谢!
展开
1个回答
展开全部
//ReadTXTFile.java 临时弄的,你运行看看,先用打开文件找你的文件,再输入数字查看每一行
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileFilter;
public class ReadTXTFile extends javax.swing.JPanel implements ActionListener {
private JButton findfile;
private JPanel jPanel1;
private JButton ok;
private JTextField input;
private JLabel inputLabel;
private JTextArea outPut;
private JLabel length;
private JLabel lengthLabel;
private JLabel viewLabel;
private JFileChooser fd;
private String body=null;
/**
* Auto-generated main method to display this
* JPanel inside a new JFrame.
*/
public static void main(String[] args) {
JFrame frame = new JFrame("读取TXT文件");
frame.getContentPane().add(new ReadTXTFile());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public ReadTXTFile() {
super();
initGUI();
}
private void initGUI() {
FileFilter ff=new FileFilter(){
public boolean accept(File pathname) {
// TODO Auto-generated method stub
return (pathname.getName().endsWith(".txt")|pathname.getName().endsWith(".TXT"))||!pathname.isFile();
}
public String getDescription() {
// TODO Auto-generated method stub
return null;
}};
try {
setPreferredSize(new Dimension(400, 300));
this.setLayout(null);
{
findfile = new JButton();
this.add(findfile);
findfile.setText("打开文件");
findfile.setBounds(18, 7, 109, 22);
findfile.addActionListener(this);
}
{
jPanel1 = new JPanel();
this.add(jPanel1);
jPanel1.setBounds(12, 223, 376, 71);
jPanel1.setBorder(BorderFactory.createTitledBorder("文件信息"));
jPanel1.setLayout(null);
{
viewLabel = new JLabel();
jPanel1.add(viewLabel);
viewLabel.setText("\u65e0\u6587\u4ef6");
viewLabel.setBounds(11, 23, 353, 15);
}
{
lengthLabel = new JLabel();
jPanel1.add(lengthLabel);
lengthLabel.setText("\u957f\u5ea6\uff1a");
lengthLabel.setBounds(11, 44, 50, 16);
}
{
length = new JLabel();
jPanel1.add(length);
length.setText("\u672a\u77e5");
length.setBounds(57, 45, 308, 15);
}
}
{
outPut = new JTextArea();
this.add(outPut);
outPut.setBounds(18, 41, 370, 145);
outPut.setEditable(false);
outPut.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
}
{
inputLabel = new JLabel();
this.add(inputLabel);
inputLabel.setText("\u8bf7\u8f93\u5165\u884c\u6570\uff1a");
inputLabel.setBounds(12, 202, 106, 16);
}
{
input = new JTextField();
this.add(input);
input.setBounds(94, 199, 88, 21);
}
{
ok = new JButton();
this.add(ok);
ok.setText("查 看");
ok.setBounds(287, 198, 96, 22);
ok.addActionListener(this);
}
} catch (Exception e) {
e.printStackTrace();
}
fd=new JFileChooser();
fd.setFileFilter(ff);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==findfile){
int state= fd.showOpenDialog(this);
if(state==0){
String filename=fd.getSelectedFile().getPath();
viewLabel.setText(filename);
try {
File f=new File(filename);
FileInputStream fi=new FileInputStream(f);
byte[] bytes=new byte[(int) f.length()];
fi.read(bytes);
body=new String(bytes);
str=body.split("\\n");
length.setText(str.length+"行");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException es) {
es.printStackTrace();
}
}
}else if(e.getSource()==ok){
if(str==null)return;
try{
int line=Integer.parseInt(input.getText());
if(line>str.length|line<=0){
outPut.setText("对不起,您输入的数字太长或太少,\n请输入"+str.length+"以内的数字");
}else{
outPut.setText(str[line-1]);
}
}catch(NumberFormatException ef){
}
}
}
String str[]=null;
}
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
import javax.swing.border.LineBorder;
import javax.swing.filechooser.FileFilter;
public class ReadTXTFile extends javax.swing.JPanel implements ActionListener {
private JButton findfile;
private JPanel jPanel1;
private JButton ok;
private JTextField input;
private JLabel inputLabel;
private JTextArea outPut;
private JLabel length;
private JLabel lengthLabel;
private JLabel viewLabel;
private JFileChooser fd;
private String body=null;
/**
* Auto-generated main method to display this
* JPanel inside a new JFrame.
*/
public static void main(String[] args) {
JFrame frame = new JFrame("读取TXT文件");
frame.getContentPane().add(new ReadTXTFile());
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public ReadTXTFile() {
super();
initGUI();
}
private void initGUI() {
FileFilter ff=new FileFilter(){
public boolean accept(File pathname) {
// TODO Auto-generated method stub
return (pathname.getName().endsWith(".txt")|pathname.getName().endsWith(".TXT"))||!pathname.isFile();
}
public String getDescription() {
// TODO Auto-generated method stub
return null;
}};
try {
setPreferredSize(new Dimension(400, 300));
this.setLayout(null);
{
findfile = new JButton();
this.add(findfile);
findfile.setText("打开文件");
findfile.setBounds(18, 7, 109, 22);
findfile.addActionListener(this);
}
{
jPanel1 = new JPanel();
this.add(jPanel1);
jPanel1.setBounds(12, 223, 376, 71);
jPanel1.setBorder(BorderFactory.createTitledBorder("文件信息"));
jPanel1.setLayout(null);
{
viewLabel = new JLabel();
jPanel1.add(viewLabel);
viewLabel.setText("\u65e0\u6587\u4ef6");
viewLabel.setBounds(11, 23, 353, 15);
}
{
lengthLabel = new JLabel();
jPanel1.add(lengthLabel);
lengthLabel.setText("\u957f\u5ea6\uff1a");
lengthLabel.setBounds(11, 44, 50, 16);
}
{
length = new JLabel();
jPanel1.add(length);
length.setText("\u672a\u77e5");
length.setBounds(57, 45, 308, 15);
}
}
{
outPut = new JTextArea();
this.add(outPut);
outPut.setBounds(18, 41, 370, 145);
outPut.setEditable(false);
outPut.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
}
{
inputLabel = new JLabel();
this.add(inputLabel);
inputLabel.setText("\u8bf7\u8f93\u5165\u884c\u6570\uff1a");
inputLabel.setBounds(12, 202, 106, 16);
}
{
input = new JTextField();
this.add(input);
input.setBounds(94, 199, 88, 21);
}
{
ok = new JButton();
this.add(ok);
ok.setText("查 看");
ok.setBounds(287, 198, 96, 22);
ok.addActionListener(this);
}
} catch (Exception e) {
e.printStackTrace();
}
fd=new JFileChooser();
fd.setFileFilter(ff);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==findfile){
int state= fd.showOpenDialog(this);
if(state==0){
String filename=fd.getSelectedFile().getPath();
viewLabel.setText(filename);
try {
File f=new File(filename);
FileInputStream fi=new FileInputStream(f);
byte[] bytes=new byte[(int) f.length()];
fi.read(bytes);
body=new String(bytes);
str=body.split("\\n");
length.setText(str.length+"行");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException es) {
es.printStackTrace();
}
}
}else if(e.getSource()==ok){
if(str==null)return;
try{
int line=Integer.parseInt(input.getText());
if(line>str.length|line<=0){
outPut.setText("对不起,您输入的数字太长或太少,\n请输入"+str.length+"以内的数字");
}else{
outPut.setText(str[line-1]);
}
}catch(NumberFormatException ef){
}
}
}
String str[]=null;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询