⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 replacestring.java

📁 可以替换同一目录下文本文件中某一字符串为新串
💻 JAVA
字号:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
import java.io.*;

class ReplaceString extends JFrame
{
	//程序中用到的字符串变量
	private String strFile = new String("");
	private String strOld = new String("");
	private String strNew = new String();
	private String strContent = new String("");
	//初始化各个控件
	private JLabel lbFile = new JLabel("文件名:");
	private JLabel lbOld = new JLabel("被替换的字符串:");
	private JLabel lbNew = new JLabel("替换成新串:") ;
	private JTextField tfFilename = new JTextField();
	private JTextField tfOld = new JTextField();
	private JTextField tfNew= new JTextField();

	private JPanel jpCenter = new JPanel(new GridLayout(3,2));
	private JPanel jpBottom = new JPanel();
	private JButton btnOK = new JButton("开始替换");
	ReplaceString()
	{
		super("字符串替换");
		this.setResizable(false);
		//设置字体属性
		Font ft = new Font("宋体",0,14);
		lbFile.setForeground(new Color(0,0,0));
		lbFile.setFont(ft);
		lbOld.setFont(ft);
		lbOld.setForeground(new Color(0,0,0));
		lbNew.setForeground(new Color(0,0,0));
		lbNew.setFont(ft);
		//把控件加入到中间的面板
		jpCenter.add(lbFile);
		jpCenter.add(tfFilename);
		jpCenter.add(lbOld);
		jpCenter.add(tfOld);
		jpCenter.add(lbNew);
		jpCenter.add(tfNew);
		//为按钮添加监听器
		btnOK.addActionListener(new OKListener());
		//把按钮加进底部的面板
		jpBottom.add(btnOK);
		//整体布局
		setLayout(new BorderLayout());
		add(Box.createVerticalStrut(30),BorderLayout.NORTH);
		add(jpCenter,BorderLayout.CENTER);
		add(Box.createHorizontalStrut(30),BorderLayout.WEST);
		add(Box.createHorizontalStrut(30),BorderLayout.EAST);
		add(jpBottom,BorderLayout.SOUTH);
		//显示窗体
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(500,150);
		setVisible(true);
	}

	class OKListener implements  ActionListener
	{
		 public void actionPerformed(ActionEvent event)
		 {
		 	strContent = "";//清空内容字符串
		 	//以下获得各个文本编辑框的输入文本
		 	strFile = tfFilename.getText();
		 	strOld = tfOld.getText();
		 	strNew = tfNew.getText();
		 	//进行合法性判断
		 	if(strFile.equals(""))
		 	{
		 		JOptionPane.showMessageDialog(null,"文件名不可以为空!", "Error!",JOptionPane.ERROR_MESSAGE);
				return;
		 	}
		 	try
		 	{
		 		//打开源文件,读取进字符串strcontent中
		 		BufferedReader br = new BufferedReader(new FileReader(strFile));
		 		String temp = new String("");
		 		while((temp = br.readLine()) != null)
		 		{
		 			strContent += temp + "\r\n";
		 		}
		 		br.close();
		 		//修改字符串的内容
		 		strContent = strContent.replaceAll(strOld,strNew);
		 		//打开源文件,将修改后的字符串写回去
		 		BufferedWriter bw = new BufferedWriter(new FileWriter("new_"+strFile));
		 		bw.write(strContent);
		 		bw.close();
		 		//提示用户操作成功
			 	JOptionPane.showMessageDialog(null,"成功替换! " + strFile, "OK!",JOptionPane.DEFAULT_OPTION);
	 			return;
		 	}
		 	catch(FileNotFoundException e)
		 	{
		 		JOptionPane.showMessageDialog(null,"文件没找到!", "Error!",JOptionPane.ERROR_MESSAGE);
				return;
		 	}
		 	catch(IOException e2)
		 	{
		 		JOptionPane.showMessageDialog(null,"UnExpected Error!", "Error!",JOptionPane.ERROR_MESSAGE);
		 		return;
		 	}
	 	}
	}

	public static void main(String[] args)
	{
		new ReplaceString();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -