Java Frame 窗口读取 文本文件代码

  
/**
 * 读取文本文件
 * JavaAlpha
 */

import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class jtxtfm {
	public static void main(String args[]) {
		jtxtfrm fm = new jtxtfrm(); //这里是程序开始执行的地方,新建一个窗口
	}
}

class jtxtfrm extends Frame implements ActionListener {
	FileDialog	op, sv;//文件对话框
	Button		btn1, btn2, btn3;//按钮
	TextArea	tarea;//文本框

	jtxtfrm() {
		super("读写文件");//标题
		setLayout(null);//布局
		setBackground(Color.cyan);//背景色
		setSize(600, 300);//设置大学
		setVisible(true);//设置可见
		btn1 = new Button("打开");//创建按钮
		btn2 = new Button("保存");
		btn3 = new Button("关闭");
		tarea = new TextArea("");//创建文本框
		add(btn1);//添加按钮
		add(btn2);
		add(btn3);
		add(tarea);
		tarea.setBounds(30, 50, 460, 220);//设置文本框位置
		btn1.setBounds(520, 60, 50, 30);
		btn2.setBounds(520, 120, 50, 30);
		btn3.setBounds(520, 180, 50, 30);
		op = new FileDialog(this, "打开", FileDialog.LOAD);//新建对话框
		sv = new FileDialog(this, "保存", FileDialog.SAVE);
		btn1.addActionListener(this);//设置按钮点击监听事件
		btn2.addActionListener(this);
		btn3.addActionListener(this);
		addWindowListener(new WindowAdapter() {//关闭事件处理
			public void windowClosing(WindowEvent e) {
				setVisible(false);//设置不可见
				System.exit(0);//程序关闭
			}
		});
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn1) {//按钮1事件处理
			String str;
			op.setVisible(true);//设置可见
			try {
				File f1 = new File(op.getDirectory(), op.getFile());//打开文件
				FileReader fr = new FileReader(f1);
				BufferedReader br = new BufferedReader(fr);//读取文件
				tarea.setText("");//设置文本框内容为空
				while ((str = br.readLine()) != null)
					tarea.append(str + '\n');//文本框添加读取的文件内容
				fr.close();//关闭读取
			} catch (Exception e1) {//如果有错误,这里进行处理
				e1.printStackTrace();//打印错误信息
			}
		}

		if (e.getSource() == btn2) {//按钮1事件处理
			sv.setVisible(true);
			try {
				File f1 = new File(sv.getDirectory(), sv.getFile());
				FileWriter fw = new FileWriter(f1);
				BufferedWriter bw = new BufferedWriter(fw);
				String gt = tarea.getText();//获取文本框内容
				bw.write(gt, 0, gt.length());//将文本框内容写入文件
				bw.flush();//真正的写入文件
				fw.close();//关闭写入
			} catch (Exception e2) {//错误处理
				e2.printStackTrace();//打印错误信息
			}
		}

		if (e.getSource() == btn3) {//按钮3事件处理
			System.exit(0);//关闭程序
		}
	}
}

Java中,你可以使用Swing或JavaFX等GUI库来创建界面,然后利用文件I/O操作来读取和修改文本文件的内容。下面是一个简单的例子,展示如何创建一个按钮,当点击时会替换文本文件的第二行内容: ```java import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.charset.StandardCharsets; public class Main { public static void main(String[] args) { // 创建一个新的窗口 JFrame frame = new JFrame("File Replace"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 创建一个面板 JPanel panel = new JPanel(); frame.add(panel); // 创建一个按钮 JButton button = new JButton("Replace File Line"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String lineToReplace = "New Line Content"; // 要替换成的新内容 try { // 读取原文件 File file = new File("path_to_your_file.txt"); // 替换为你的文件路径 String content = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8).get(1); // 获取第二行(索引从0开始) // 替换第二行 content = content.replaceFirst(content, lineToReplace); // 写回文件 Files.write(file.toPath(), Arrays.asList(content), StandardCharsets.UTF_8); JOptionPane.showMessageDialog(frame, "Line replaced successfully!"); } catch (IOException ex) { JOptionPane.showMessageDialog(frame, "Error replacing line: " + ex.getMessage()); } } }); panel.add(button); frame.pack(); frame.setVisible(true); } } ``` **相关问题:** 1. Java Swing和JavaFX有什么区别?它们各自适合什么样的场景? 2. 在Java中如何处理文件I/O异常,例如`FileNotFoundException`? 3. 如果文件不存在,这个例子应该如何处理?
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值