标签:
1 class RadioHere extends JFrame implements ActionListener 2 { 3 private JTextArea ta=new JTextArea(10,20); 4 private JFileChooser jfc=new JFileChooser(new File(".")); 5 private JButton bOpen,bSave; 6 public RadioHere() 7 { 8 super("文档选择框应用程序"); 9 JScrollPane ps=new JScrollPane(ta); 10 bOpen=new JButton("选取"); 11 bSave=new JButton("存盘"); 12 setLayout(new FlowLayout(FlowLayout.CENTER,15,10)); 13 add(ps); 14 add(bOpen); 15 add(bSave); 16 bOpen.addActionListener(this); 17 bSave.addActionListener(this); 18 setVisible(true); 19 setSize(600,560); 20 } 21 public static void main(String[] args) 22 { 23 RadioHere frm=new RadioHere(); 24 frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 25 } 26 public void actionPerformed(ActionEvent e) 27 { 28 JButton jbt=(JButton)e.getSource(); 29 if(jbt==bOpen) 30 { 31 int status=jfc.showOpenDialog(this); 32 if(status!=JFileChooser.APPROVE_OPTION) 33 ta.setText("没有选择文件"); 34 else { 35 try{ 36 File file1=jfc.getSelectedFile(); 37 Scanner scan=new Scanner(file1); 38 String info=""; 39 while(scan.hasNext()) 40 { 41 String str=scan.nextLine(); 42 info+=str+"\r\n"; 43 } 44 ta.setText(info); 45 } 46 catch(FileNotFoundException ex){ 47 48 } 49 } 50 } 51 else{ 52 int re=jfc.showSaveDialog(this); 53 if(re==JFileChooser.APPROVE_OPTION) 54 { 55 try{ 56 File file2=jfc.getSelectedFile(); 57 FileOutputStream f=new FileOutputStream(file2); 58 BufferedOutputStream out=new BufferedOutputStream(f); 59 byte[] b=(ta.getText()).getBytes(); 60 out.write(b,0,b.length); 61 out.close(); 62 } 63 catch(IOException ie) 64 { 65 } 66 } 67 } 68 } 69 }
可以读取txt和cpp文档,并且能够将textarea里面的内容进行保存
标签:
原文地址:http://www.cnblogs.com/sytu/p/java.html