java程式檔案讀取與儲存執行個體代碼

來源:互聯網
上載者:User

標籤:

 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裡面的內容進行儲存

java程式檔案讀取與儲存執行個體代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.