java file (IO) programming--Simple notepad development

Source: Internet
Author: User


1 public class NotePad extends JFrame implements ActionListener{
Two
3 / / define the required components
4 jtextarea JTA = null; / / multiline text box
Five
6 jmenubar JMB = null; / / menu bar
7 JMenu jm1 = null; / / menu
8 jmuitem jmi1 = null, jmi2 = null; / / menu item
Nine
10     public static void main(String[] args) {
11         NotePad np=new NotePad();
Twelve
13}
Fourteen
15 public notepad() {/ / constructor
Sixteen
17 JTA = new jtextarea(); / / create JTA
18         jmb=new JMenuBar();
19 jm1 = new JMenu ("file");
20 jm1.setmnemonic ('f '); / / set mnemonic
Twenty-one
22 jmi1 = new jmenuitem ("open", new imageicon ("imag_3. JPG");
23 jmi1. Addactionlistener (this); / / register to listen
24         jmi1.setActionCommand("open");
Twenty-five
26 jmi2 = new jmenuitem ("save");
27         jmi2.addActionListener(this);
28         jmi2.setActionCommand("save");
Twenty-nine
30 this. Setjmenubar (JMB); / / add
Thirty-one
32 JMB. Add (jm1); / / put the menu into the menu bar
Thirty-three
34 jm1. Add (jmi1); / / put item into menu
35         jm1.add(jmi2);
Thirty-six
37 this. Add (JTA); / / put it into JFrame
Thirty-eight
39         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
40         this.setSize(400,300);
41 this.settitle ("NOTEPAD");
42         this.setIconImage((new ImageIcon("imag_2.jpg")).getImage());
43         this.setVisible(true);
44}
Forty-five
46     @Override
47     public void actionPerformed(ActionEvent arg0) {
48 / / judge which menu is selected
49         if(arg0.getActionCommand().equals("open")){
Fifty
51 / / Jfilechooser, create a file selection component
52             JFileChooser jfc1=new JFileChooser();
53 jfc1.setdialogtitle ("please select File "); / / set the name
Fifty-four
55 jfc1.showopendialog (null); / / default
56 jfc1. Setvisible (true); / / display
Fifty-seven
58 / / get the full path of the file selected by the user
59             String filename=jfc1.getSelectedFile().getAbsolutePath();
Sixty
61             FileReader fr=null;
62             BufferedReader br=null;
Sixty-three
64             try {
65                 fr=new FileReader(filename);
66                 br=new BufferedReader(fr);
Sixty-seven
68 / / read the information from the file and display it to JTA
69                 String s="";
70                 String allCon="";
71 while ((s = br. Readline())! = null) {/ / loop to read the file. If s is not empty, it is not read
72                     allCon+=s+"\r\n";
73                 }
Seventy-four
75 JTA. Settext (allcon); / / place it in JTA
Seventy-six
77             } catch (Exception e) {
78                 e.printStackTrace();
79             }finally{
Eighty
81                 try {
82                     fr.close();
83                     br.close();
84                 } catch (Exception e) {
85                     e.printStackTrace();
86                 }
87             }
88         }else if(arg0.getActionCommand().equals("save")){
89 / / the save dialog box appears
90             JFileChooser jfc2=new JFileChooser();
91 jfc2.setdialogtitle ("save as "";
92 jfc2.showsavedialog (null); / / display by default
93             jfc2.setVisible(true);
Ninety-four
95 / / get where the user wants to save the file and the full path of the file
96             String filename2=jfc2.getSelectedFile().getAbsolutePath();
Ninety-seven
98 / / prepare to write to the specified file
99             FileWriter fw=null;
100             BufferedWriter bw=null;
One hundred and one
102             try {
103                 fw=new FileWriter(filename2);
104                 bw=new BufferedWriter(fw);
One hundred and five
106                 bw.write(this.jta.getText());
107             } catch (Exception e) {
108                 e.printStackTrace();
109             }finally{
110                 try {
111                     bw.close();
112                 } catch (IOException e) {
113                     e.printStackTrace();
114                 }
115             }
116         }
117}
118} 


The results are as follows:






Click the File button, click the Open menu item, select a text file, the effect is as follows:






When opened, the content is displayed as follows:






Modify the content slightly, save as a file named SSS, the effect is as follows:






java file (IO) programming--Simple notepad development


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.