zoukankan      html  css  js  c++  java
  • 20110610上午java考试复数题

    一道大题
    (1) 在程序界面由上至下依次设按钮A、文本A、按钮B、文本B、按钮C
    (2) 按按钮A后,读取外部文件"input.txt",将其显示在文本区A中
    (3) 按按钮B后,找出文本A中的各英文单词(注意剔除其中的标点),将其按照字典序降序排序后在文本B中逐行输出
    (4) 按按钮C后,将文本区B中的内容写入外部文件"output.txt"

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;

    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;

    public class Exam {

        private JButton btnA;
        private JTextArea txtAreaA;
        private JScrollPane jspA;
        private JButton btnB;
        private JTextArea txtAreaB;
        private JScrollPane jspB;
        private JButton btnC;

        public Exam(){
            JFrame frame = new JFrame("2011.06.10 Exam");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocationRelativeTo(null);
            frame.setSize(400, 300);
            
            JPanel pan = new JPanel();
            BoxLayout b = new BoxLayout(pan, BoxLayout.Y_AXIS);
            pan.setLayout(b);

            btnA = new JButton("Button A");
            btnA.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    FileInputStream fis = null;
                    InputStreamReader isr = null;
                    try{
                        String path = System.getProperty("user.dir");
                        path = path+"/input.txt";
                        //String path = "c://input.txt";
                        File file = new File(path);
                        fis = new FileInputStream(file);
                        isr = new InputStreamReader(fis);
                        StringBuilder textA = new StringBuilder();
                        int v ;
                        while(true){
                            v = isr.read();
                            if(v==-1){
                                break;
                            }
                            textA.append((char)v);
                        }
                        txtAreaA.setText(textA.toString());
                    }catch (Exception ee) {
                        ee.printStackTrace();
                    }finally{
                        try{
                            if(null!=isr)isr.close();
                        }catch(Exception ee){
                            ee.printStackTrace();
                        }
                        try{
                            if(null!=fis)fis.close();
                        }catch(Exception ee){
                            ee.printStackTrace();
                        }
                    }
                }
            });
            txtAreaA = new JTextArea();
            jspA = new JScrollPane();
            jspA.add(txtAreaA);
            jspA.setViewportView(txtAreaA);
            btnB = new JButton("Button B");
            btnB.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    String text = txtAreaA.getText();
                    String strArr[] = text.split("[^\\w]");
                    List<String> strList = new ArrayList<String>();
                    String s;
                    for(int i=0;i<strArr.length;i++){
                        s = strArr[i];
                        if(!"".equals(s)){
                            strList.add(s.replace("\n", ""));
                        }
                    }
                    Collections.sort(strList);
                    StringBuilder strText = new StringBuilder();
                    for(int j=strList.size()-1;j>=0;j--){
                        strText.append(strList.get(j)).append("\n");
                    }
                    txtAreaB.setText(strText.toString());
                }
            });
            txtAreaB = new JTextArea();
            jspB = new JScrollPane();
            jspB.add(txtAreaB);
            jspB.setViewportView(txtAreaB);
            btnC = new JButton("Button C");
            btnC.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    String textB = txtAreaB.getText();
                    textB = textB.replace("\n", "\r");
                    FileOutputStream fos = null;
                    OutputStreamWriter osw = null;
                    try{
                        String path = System.getProperty("user.dir");
                        path = path+"/output.txt";
                        //String path = "c://output.txt";
                        File file = new File(path);
                        fos = new FileOutputStream(file);
                        osw = new OutputStreamWriter(fos);
                        osw.write(textB);
                    }catch (Exception ee) {
                        ee.printStackTrace();
                    }finally{
                        try{
                            if(null!=osw)osw.close();
                        }catch(Exception ee){
                            ee.printStackTrace();
                        }
                        try{
                            if(null!=fos)fos.close();
                        }catch(Exception ee){
                            ee.printStackTrace();
                        }
                    }
                    JOptionPane.showMessageDialog(null,"write output.txt over");
                }
            });
            pan.add(btnA);
            pan.add(jspA);
            pan.add(btnB);
            pan.add(jspB);
            pan.add(btnC);

            frame.add(pan);
            frame.setVisible(true);
        }

        /**
         * @param args
         */
        public static void main(String[] args) {
            new Exam();
        }

    }

  • 相关阅读:
    VBA操作IE
    Eclipse中Git图标表示内容
    sqldeveloper更改语言设定
    VBA-FileToFileUpdate
    VBA-UTF-8文件的操作
    Null项目参与排序
    阿里云的学生机如何开放全部端口
    .net core3.1 webapi + vue + element-ui upload组件实现文件上传
    .net core控制台使用log4net
    vue2.x中使用三元表达式绑定class的时候遇到的坑
  • 原文地址:https://www.cnblogs.com/SunDexu/p/2767127.html
Copyright © 2011-2022 走看看