swing 插件实现简单的文件读取与保存

    xiaoxiao2021-03-25  77

    图像化界面实现对文件的操作,编程实现GUI菜单栏快速访问

    package notepade; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.KeyStroke; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class Mynotepade extends JFrame{ JMenuBar bar= new JMenuBar();//定义菜单栏 JMenuItem item_open=new JMenuItem("open(O)"); JMenuItem item_save_as=new JMenuItem("save as(S)"); JMenu menu_file=new JMenu("file(F)"); JTextArea j=new JTextArea(); JScrollPane js=new JScrollPane(j); public Mynotepade() { this.setSize(500,500); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setLocationRelativeTo(null); init(); this.setJMenuBar(bar); this.getContentPane().add(js); } private void init(){ item_open.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); if (chooser.showOpenDialog(Mynotepade.this)==JFileChooser.APPROVE_OPTION) {// File file = chooser.getSelectedFile(); j.setText(file.getName()+":"+file.getPath()+"\n"+file.length()); readFile(file); }; } }); item_save_as.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); if (chooser.showSaveDialog(Mynotepade.this)==JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); writeFile(file.getPath()); } } }); menu_file.setMnemonic(KeyEvent.VK_F);//Alt+F键盘快速访问 item_open.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,KeyEvent.CTRL_MASK));//Ctrl+O快速访问 item_save_as.setMnemonic(KeyEvent.VK_S);//英文状态下输入s快速访问 menu_file.add(item_open); menu_file.add(item_save_as); bar.add(menu_file); } public void readFile(File file){//读文件 BufferedReader bReader; try { bReader=new BufferedReader(new FileReader(file)); StringBuffer sBuffer=new StringBuffer(); String str; while((str=bReader.readLine())!=null){ sBuffer.append(str+'\n'); System.out.println(str); } j.setText(sBuffer.toString()); } catch (Exception e) { // TODO: handle exception } } public void writeFile(String savepath){//写文件 FileOutputStream fos= null; try { fos=new FileOutputStream(savepath); fos.write(j.getText().getBytes()); fos.close(); System.out.println("已保存"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } j.getText(); } public void copyFile(File file){//复制文件 File to=new File(file.getAbsolutePath()+"_copy"); if (file.isFile()) { byte[] buf = new byte[1024];//字节流 int length=0; try { FileInputStream in=new FileInputStream(file); FileOutputStream out=new FileOutputStream(to); while((length=in.read(buf))>0){ out.write(buf,0,length); } out.flush(); in.close(); out.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { // TODO Auto-generated method stub try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());//适应系统的显示风格 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { // TODO Auto-generated catch block e.printStackTrace(); } Mynotepade mynotepade= new Mynotepade(); mynotepade.setVisible(true); } }
    转载请注明原文地址: https://ju.6miu.com/read-16565.html

    最新回复(0)