- package Google;
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- import javax.swing.*;
- import javax.swing.border.*;
- public class Match_File extends JFrame implements ActionListener {
- private TextArea Txt;
- private FileDialog saveDia, openDia;
- private File file;
- JButton Open_File, Save_File, Exit;
- JLabel JL1, JL2,FileInfo,FilePath;
- JTextField JT1;
- public Match_File() {
- this.setLayout(new FlowLayout(FlowLayout.LEFT));
- Txt = new TextArea("请点击打开文件,导入数据", 10, 42);
- openDia = new FileDialog(this, "打开", FileDialog.LOAD);
- saveDia = new FileDialog(this, "保存", FileDialog.SAVE);
- add(showBorder1(new TitledBorder("文件类型")));
- this.add(Txt);
- add(showBorder2());
- int width = Toolkit.getDefaultToolkit().getScreenSize().width;
- int height = Toolkit.getDefaultToolkit().getScreenSize().height;
- this.setLocation(width / 2 - 300, height / 2 - 150);
- this.setVisible(true);
- this.setSize(335, 310);
- this.setResizable(false);
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- this.setTitle("词条过滤器");
- }
- public JPanel showBorder2() {
- JPanel JP = new JPanel();
- JP.setLayout(new FlowLayout());
- FileInfo = new JLabel(" 文件路径: ");
- FilePath = new JLabel(" ");
- JT1 = new JTextField(35);
- JT1.setEditable(false);
- JP.add(FileInfo);
- JP.add(JT1);
- return JP;
- }
- public JPanel showBorder1(Border B) {
- JPanel JP = new JPanel();
- JP.setLayout(new FlowLayout());
- Open_File = new JButton(" 打开文件 ");
- Open_File.addActionListener(this);
- JL1 = new JLabel(" ");
- Save_File = new JButton(" 保存文件 ");
- Save_File.addActionListener(this);
- JL2 = new JLabel(" ");
- Exit = new JButton("退出");
- Exit.addActionListener(this);
- JP.add(Open_File);
- JP.add(JL1);
- JP.add(Save_File);
- JP.add(JL2);
- JP.add(Exit);
- JP.setBorder(B);
- return JP;
- }
- public static void main(String[] args) {
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
- .getInstalledLookAndFeels()) {
- if ("Windows Classic".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new Match_File().setVisible(true);
- }
- });
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() == this.Open_File) {
- openDia.setVisible(true);
- String dirPath = openDia.getDirectory();
- String fileName = openDia.getFile();
- System.out.println(dirPath + "...." + fileName);
- if (dirPath == null || fileName == null)
- return;
- Txt.setText("");
- File file = new File(dirPath, fileName);
- String Path = dirPath+fileName;
- FileInfo.setText("文件打开路径:");
- //FilePath.setText(Path);
- JT1.setText(Path);
- try {
- BufferedReader bufr = new BufferedReader(new InputStreamReader(
- new FileInputStream(file), "gbk"));
- String line = null;
- while ((line = bufr.readLine()) != null) {
- String[] New_Tmp = line.split("=");
- if (New_Tmp.length > 1) {
- Txt.append(New_Tmp[1] + "\\r\\n");
- } else {
- Txt.append(line + "\\r\\n");
- }
- }
- bufr.close();
- } catch (IOException ex) {
- throw new RuntimeException("读取失败");
- }
- } else if (e.getSource() == this.Save_File) {
- if (file == null) {
- saveDia.setVisible(true);
- String dirPath = saveDia.getDirectory();
- String fileName = saveDia.getFile();
- if (dirPath == null || fileName == null)
- {
- return;
- }
- file = new File(dirPath, fileName);
- String Path = dirPath+fileName;
- FileInfo.setText("文件保存路径:");
- //FilePath.setText(Path);
- JT1.setText(Path);
- }
- try {
- OutputStreamWriter write = new OutputStreamWriter(
- new FileOutputStream(file), "gbk");
- BufferedWriter bufw = new BufferedWriter(write);
- String text = Txt.getText();
- bufw.write(text);
- bufw.close();
- JOptionPane.showMessageDialog(null, "文件已保存");
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- } else {
- JOptionPane.showMessageDialog(null, "程序现在退出");
- System.exit(0);
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1612201514238.html
来源: http://www.codesnippet.cn/detail/1612201514238.html