zoukankan      html  css  js  c++  java
  • java程序文件读取与保存实例代码

     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+"
    ";
    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里面的内容进行保存

  • 相关阅读:
    Codeforces Round #365 (Div. 2) D
    Codeforces Round #414 C. Naming Company
    Codeforces Round #365 (Div. 2) B
    LA 6893 The Big Painting(矩阵Hash)
    Gym100783C Golf Bot(FFT)
    POJ 2481 Cows(树状数组)
    POJ 2352 Stars
    POJ 2299 Ultra-QuickSort(树状数组+离散化)
    LightOJ 1341 Aladdin and the Flying Carpet(唯一分解定理)
    LightOJ 1356 Prime Independence(质因数分解+最大独立集+Hopcroft-Carp)
  • 原文地址:https://www.cnblogs.com/sytu/p/java.html
Copyright © 2011-2022 走看看