//作用,在一个大项目中,查找和替换某些内容。 Search.java /* *2010-12-13 23:25 *作者:yang *QQ:519870018 */ package classes; import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.util.ArrayList; public class Search extends JFrame implements ActionListener { JLabel lblPath, lblExtendsName, lblSearch, lblReplace; JTextField txtPath, txtExtendsName, txtSearch, txtReplace; JButton btnGetPath, btnSearch, btnReplace; JPanel pnMain; JFileChooser fileChooser; static ArrayList<String> allFileNames = new ArrayList<String>(); static ArrayList<String> resultFileNames = new ArrayList<String>(); static ArrayList<String> replaceFileNames = new ArrayList<String>(); String pathTxt = ""; String extendsName = ""; String searchTxt = ""; String replaceTxt = ""; public Search() { fileChooser = new JFileChooser("."); fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); fileChooser.setDialogTitle("打开文件夹"); lblPath = new JLabel("搜索路径:"); lblExtendsName = new JLabel("扩展名:"); lblSearch = new JLabel("搜索内容:"); lblReplace = new JLabel("替换内容:"); txtPath = new JTextField(); txtExtendsName = new JTextField(); txtSearch = new JTextField(); txtReplace = new JTextField(); btnGetPath = new JButton("打开"); btnSearch = new JButton("搜索"); btnReplace = new JButton("替换"); btnGetPath.addActionListener(this); btnSearch.addActionListener(this); btnReplace.addActionListener(this); lblPath.setBounds(40, 50, 75, 25); txtPath.setBounds(100, 50, 180, 25); lblExtendsName.setBounds(40, 85, 75, 25); txtExtendsName.setBounds(100, 85, 180, 25); lblSearch.setBounds(40, 120, 75, 25); txtSearch.setBounds(100, 120, 180, 25); lblReplace.setBounds(40, 155, 75, 25); txtReplace.setBounds(100, 155, 180, 25); btnGetPath.setBounds(290, 50, 60, 20); btnSearch.setBounds(3, 200, 60, 20); btnReplace.setBounds(295, 200, 60, 20); pnMain = new JPanel(); pnMain.setLayout(null); pnMain.add(lblPath); pnMain.add(txtPath); pnMain.add(lblExtendsName); pnMain.add(txtExtendsName); pnMain.add(lblSearch); pnMain.add(txtSearch); pnMain.add(lblReplace); pnMain.add(txtReplace); pnMain.add(btnGetPath); pnMain.add(btnSearch); pnMain.add(btnReplace); //窗体处理 this.getContentPane().add(pnMain); pnMain.setBackground(new Color(228, 244, 255)); //设置面板背景颜色 this.setUndecorated(true); this.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG); this.setTitle("yang文件处理工具"); this.setSize(370, 260); this.setLocationRelativeTo(null); this.setResizable(false); this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); this.setVisible(true); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { //处理程序结束事件 System.exit(0); } }); } public void getAllFileNames(String dirName, String[] extendsNames) { File file = new File(dirName); if (file.exists()) { //判断是否为文件 if (file.isFile()) { //输出文件名 file.setReadable(true); file.setWritable(true); for (String s : extendsNames) { if (dirName.endsWith(s)) { allFileNames.add(dirName); return; } } return; } if (file.isDirectory()) { //输出目录名 file.setReadable(true); file.setWritable(true); String[] allContent = file.list(); if (allContent == null) { return; } for (String i : allContent) { //递归 getAllFileNames(dirName + "/" + i, extendsNames); } } } } public void search(String searchContent) { ArrayList<String> logLines = new ArrayList<String>(); for (int i = 0; i < allFileNames.size(); i++) { try { String fileName = allFileNames.get(i); File file = new File(fileName); BufferedReader reader = new BufferedReader(new FileReader(file)); String line = reader.readLine(); int count = 1; boolean isAdd = true; while (line != null) { int index = line.indexOf(searchContent); if (index >= 0 && isAdd == true) { resultFileNames.add(fileName); isAdd = false; //System.out.println(fileName); //break; } if (index >= 0) { String newLine = "---- 文件:" + fileName + "---- 第" + count + "行 ---- 内容:" + line; logLines.add(newLine); } line = reader.readLine(); count++; } reader.close(); } catch (Exception e) { e.printStackTrace(); } } String oneLine = "/n---------- 全部文件共" + allFileNames.size() + "个 ----------"; printFile("logs", getTodayDate() + ".txt", allFileNames, oneLine, true); oneLine = "/n---------- 扫描文件共" + resultFileNames.size() + "个 ----------"; printFile("logs", getTodayDate() + ".txt", resultFileNames, oneLine, true); oneLine = "/n---------- 共" + logLines.size() + "行 ----------"; printFile("logs", getTodayDate() + ".txt", logLines, oneLine, true); } public void replace(String searchContent, String replaceContent) { BufferedReader reader = null; ArrayList<String> fileLines = new ArrayList<String>(); ArrayList<String> logLines = new ArrayList<String>(); for (int i = 0; i < resultFileNames.size(); i++) { fileLines.clear(); try { String filePathAndName = resultFileNames.get(i); File file = new File(filePathAndName); reader = new BufferedReader(new FileReader(file)); String line = reader.readLine(); int count = 1; while (line != null) { int index = line.indexOf(searchContent); if (index >= 0) { line = line.replace(searchContent, replaceContent); String newLine = "---- 文件:" + filePathAndName + "---- 第" + count + "行 ---"; logLines.add(newLine); } fileLines.add(line); line = reader.readLine(); count++; } reader.close(); String filePath = file.getParent(); filePath = filePath.replace(new File(pathTxt).getParent(), "output"); String fileName = file.getName(); //System.out.println("filePath=" + filePath + " fileName=" + fileName); printFile(filePath, fileName, fileLines, null, false); replaceFileNames.add(filePathAndName); } catch (Exception e) { if (reader != null) { try { reader.close(); } catch (Exception ex) { ex.printStackTrace(); } } e.printStackTrace(); } } String oneLine = "/n---------- 替换文件共" + replaceFileNames.size() + "个 ----------"; printFile("logs", getTodayDate() + ".txt", replaceFileNames, oneLine, true); oneLine = "/n---------- 共" + logLines.size() + "行 ----------"; printFile("logs", getTodayDate() + ".txt", logLines, oneLine, true); } public void actionPerformed(ActionEvent e) { JButton source = (JButton) e.getSource(); if (source == btnGetPath) { txtPath.setText(openDir()); } else { pathTxt = txtPath.getText().trim(); if (pathTxt.length() == 0) { JOptionPane.showMessageDialog(null, "路径不能为空"); return; } File file = new File(pathTxt); if (!file.exists()) { JOptionPane.showMessageDialog(null, " 不是有效的路径或文件!"); return; } searchTxt = txtSearch.getText().trim(); if (searchTxt.length() == 0) { JOptionPane.showMessageDialog(null, "搜索内容不能为空"); return; } extendsName = txtExtendsName.getText().trim(); if (extendsName.length() == 0) { JOptionPane.showMessageDialog(null, "文件扩展名不能为空"); return; } String[] extendsNames = extendsName.split(","); allFileNames.clear(); resultFileNames.clear(); replaceFileNames.clear(); if (source == btnSearch) { printLogInfo("搜索"); getAllFileNames(pathTxt, extendsNames); search(searchTxt); JOptionPane.showMessageDialog(null, "搜索完成!"); } else { replaceTxt = txtReplace.getText().trim(); if (replaceTxt.length() == 0) { JOptionPane.showMessageDialog(null, "替换内容不能为空"); return; } printLogInfo("替换"); getAllFileNames(pathTxt, extendsNames); search(searchTxt); replace(searchTxt, replaceTxt); JOptionPane.showMessageDialog(null, "替换完成!"); } } } public void printLogInfo(String doType) { String oneLine = "/n/n/n---------- 日期:" + getTodayDate() + " 时间:" + getNowTime() + " 操作类型:" + doType + " ----------"; printFile("logs", getTodayDate() + ".txt", null, oneLine, true); oneLine = "---------- 搜索路径:" + pathTxt + " ----------"; printFile("logs", getTodayDate() + ".txt", null, oneLine, true); oneLine = "---------- 扩展名:" + extendsName + " ----------"; printFile("logs", getTodayDate() + ".txt", null, oneLine, true); oneLine = "---------- 搜索内容:" + searchTxt + " ----------"; printFile("logs", getTodayDate() + ".txt", null, oneLine, true); oneLine = "---------- 替换内容:" + replaceTxt + " ----------"; printFile("logs", getTodayDate() + ".txt", null, oneLine, true); } public String openDir() { String path = ""; int ret = fileChooser.showOpenDialog(null); if (ret == JFileChooser.APPROVE_OPTION) { path = fileChooser.getSelectedFile().getAbsolutePath(); } return path; } //取今天的日期 public String getTodayDate() { Calendar calendar = new GregorianCalendar(); int year = calendar.get(Calendar.YEAR); int month = calendar.get(Calendar.MONTH) + 1; int day = calendar.get(Calendar.DATE); String dateTime = year + "-" + month + "-" + day; return dateTime; } public String getNowTime() { Calendar calendar = new GregorianCalendar(); int hour = calendar.get(Calendar.HOUR); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND); String time = hour + ":" + minute + ":" + second; return time; } public void printFile(String path, String fileName, ArrayList<String> linesList, String oneLine, boolean append) { try { File fileDir = new File(path); if (!fileDir.exists()) { fileDir.mkdirs(); } PrintWriter pw = new PrintWriter(new FileOutputStream(path + "/" + fileName, append), true); if (oneLine != null) { pw.println(oneLine); } if (linesList != null) { for (int i = 0; i < linesList.size(); i++) { String line = linesList.get(i); pw.println(line); } } pw.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { new Search(); } } Search.bat @ECHO ......请先设置好 系统环境变量 @ECHO ......即把 java 安装目录 bin 加到 path 里 javac Search.java -d . java classes.Search pause;