一道大题(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(); }}
转载于:https://www.cnblogs.com/SunDexu/archive/2012/11/12/2767127.html
相关资源:JAVA上百实例源码以及开源项目