根据java+gui实现简单的根据文件内容搜索软件
效果展示:
1、主框架
package com.yzc.gui.frame;
import com.yzc.gui.pane.BottomPane;
import com.yzc.gui.pane.TopPane;
import javax.swing.*;
import java.awt.*;
public class MainFrame extends JFrame {
public JButton submit = new JButton("提交");
public static JTextField textField = new JTextField(30);
public static JTextField textField2 = new JTextField(30);
public MainFrame() throws Exception {
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension dimension = tk.getScreenSize();
int screenHeight = dimension.height;
int screenWidth = dimension.width;
this.setSize(500,350);
this.setBounds(screenWidth/4, screenHeight/4, screenWidth/2, screenHeight/2);
this.setBackground(Color.darkGray);
this.setVisible(true);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
this.add(new TopPane(submit, textField, textField2), BorderLayout.NORTH);
this.add(new BottomPane(submit, textField, textField2), BorderLayout.CENTER);
}
}
2.顶部框架
package com.yzc.gui.pane;
import com.yzc.gui.actionListener.ChooseActionListener;
import javax.swing.*;
public class TopPane extends JPanel {
public TopPane(JButton submit, JTextField textField, JTextField textField2) {
textField.setToolTipText("请输入路径");
textField.setEditable(false);
textField2.setToolTipText("请输出匹配符");
this.add(textField);
JButton chooser = new JButton("选择");
chooser.addActionListener(new ChooseActionListener());
this.add(chooser);
this.add(textField2);
this.add(submit);
this.add(new JButton("停止"));
}
}
3.底部框架
package com.yzc.gui.pane;
import com.yzc.gui.actionListener.MainActionListener;
import javax.swing.*;
import java.awt.*;
public class BottomPane extends JPanel {
public BottomPane(JButton submit, JTextField textField, JTextField textField2) {
JTextArea textArea = new JTextArea();
textArea.setRows(26);
textArea.setColumns(80);
textArea.setCaretPosition(0);
textArea.setSelectedTextColor(Color.blue);
submit.addActionListener(new MainActionListener(textArea, textField.getText(), textField2.getText()));
this.add(textArea);
}
}
4.文件工具类
package com.yzc.util;
import javax.swing.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* 文件工具类
* @author yangZhiChao 20230117
*/
public class FileUtil {
public static List<String> result = new ArrayList<>();
/**
* 文件读取——字符流
* @param filePath 文件路径
* @return
* @throws Exception
*/
public static String readFileReader(String filePath) throws IOException {
File file = new File(filePath);
if(!file.exists()) {
try {
file.createNewFile();
} catch (Exception e) {
e.printStackTrace();
}
return "";
} else {
StringBuffer result = new StringBuffer("");
FileReader fr = null;
BufferedReader br = null;
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
String str = null;
while ((str = br.readLine()) != null) {
result.append(str);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
br.close();
fr.close();
}
return result.toString();
}
}
/**
* 快速文件读取
* @param filePath 文件路径
* @return
* @throws IOException
*/
public static String readFileFast(String filePath) throws IOException {
String resultStr = "";
FileReader fr = null;
CharArrayWriter caw = null;
try {
fr = new FileReader(filePath);
char[] cs = new char[1024];
caw = new CharArrayWriter();
int len = 0;
while ((len = fr.read(cs)) != -1) {
caw.write(cs, 0, len);
}
resultStr = caw.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
caw.close();
fr.close();
}
return resultStr;
}
/**
* 文件读取——字节流
* @param filePath 文件路径
* @return
* @throws IOException
*/
public static String readFileInput(String filePath) throws IOException {
StringBuffer result = new StringBuffer("");
BufferedInputStream bis = null;
FileInputStream fis = null;
File file = new File(filePath);
if(!file.exists()) {
file.createNewFile();
} else {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
int a;
byte[] b = new byte[101];
while ((a = bis.read(b)) != -1) {
String str = new String(b, 0 ,a);
result.append(str);
}
}
return result.toString();
}
/**
* 打印指定路径下的所有文件以及文件夹
* @param path
* @return
*/
public static List<String> findFiles(String path) {
List<String> result = new ArrayList<>();
File file = new File(path);
File[] files = file.listFiles();
if(file.isDirectory()) { // 如果是文件夹
List<File> fileList = Arrays.asList(files);
for(File newFile : fileList) {
System.out.println(path+newFile.getName());
result.add(path+newFile.getName());
}
return result;
} else {
System.out.println(path);
result.add(path);
return result;
}
}
/**
* 根据指定字符匹配指定路径下的文件
* @param path
* @return
*/
public static List<String> findFilesWithChar(String path, String cc) throws Exception {
File file = new File(path);
if(file.isDirectory()) { // 如果是文件夹
File[] files = file.listFiles();
if(files != null && files.length > 0) {
List<File> fileList = Arrays.asList(files);
fileList.parallelStream().forEach(newFile -> {
if (newFile.isDirectory()) {
try {
findFilesWithChar(newFile.getPath(), cc);
} catch (Exception e) {
e.printStackTrace();
}
} else {
String fileContent = null;
try {
fileContent = readFileFast(newFile.getPath());
} catch (IOException e) {
e.printStackTrace();
}
if (fileContent != null && fileContent.contains(cc)) {
result.add(newFile.getPath());
}
}
});
}
System.out.println(path);
return result;
} else {
String fileContent = readFileReader(path);
if(fileContent != null && fileContent.contains(cc)) {
result.add(path);
}
return result;
}
}
}
5.文件选择窗体及事件
package com.yzc.gui.actionListener;
import com.yzc.gui.frame.MainFrame;
import com.yzc.gui.pane.TopPane;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ChooseActionListener extends Component implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser("选择");
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChooser.showOpenDialog(fileChooser);
String path = fileChooser.getSelectedFile().getAbsolutePath();
System.out.println(path);
MainFrame.textField.setText(path);
}
}
6.其他点击事件
package com.yzc.gui.actionListener;
import com.yzc.gui.frame.MainFrame;
import com.yzc.util.FileUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class MainActionListener extends Component implements ActionListener {
private JTextArea textArea;
private String text1;
private String text2;
@Override
public void actionPerformed(ActionEvent e) {
this.text1 = MainFrame.textField.getText();
this.text2 = MainFrame.textField2.getText();
StringBuffer sb = new StringBuffer("");
List<String> list = null;
try {
FileUtil.result.clear();
list = FileUtil.findFilesWithChar(text1, text2);
for(String filePath : list) {
sb.append(filePath).append("\n");
}
textArea.setText(sb.toString());
} catch (Exception exception) {
exception.printStackTrace();
}
}
public MainActionListener(JTextArea textArea, String text1, String text2) {
this.textArea = textArea;
this.text1 = text1;
this.text2 = text2;
}
public MainActionListener() {
}
}
7.main方法
package com.yzc.test;
import com.yzc.gui.frame.MainFrame;
public class Test {
public static void main(String[] args) throws Exception {
new MainFrame();
}
}