개인용 복습공간

[Java] 메모장 만들기 - 2 본문

Java

[Java] 메모장 만들기 - 2

taehwanis 2021. 5. 21. 12:35

 

 

 

메모장의 주요 기능들을
만들어보려고 한다.

 

 

 

 

 

메모장

 

메모장 '열기' 실행화면

 

 

Notepad.java 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    public void loadFile(String fn){
        
        try {
            FileReader fin = new FileReader(new File(fn));
 
            ta.setText("");
            int c= 0;
            while((c=fin.read())!=-1) {
                String s = String.valueOf((char)c);
                ta.append(s);
            }
            fin.close();
            
        } catch (IOException e) {
                e.printStackTrace();
        }
 
    }
    
    public void saveFile(String fn){
        try {
            FileWriter fout = new FileWriter(new File(fn));
 
            fout.write(ta.getText());
 
            fout.close();
            
        } catch (IOException e) {
                e.printStackTrace();
        }
    }
cs

Notepad.java에 loadFile메서드와 saveFile메서드를 추가했다. 여러 가지 방법이 있겠지만 간단하게 FileReader와 FileWriter로 기능을 만들었다. 메서드의 매개변수로는 파일의 path를 가져온다.

 

 

NoteActionListener.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
 
public class NoteActionListener implements ActionListener {
    Notepad frm;
    String cmd;
 
    public NoteActionListener(Notepad frm) {
        this.frm = frm;
    }
 
    @Override
    public void actionPerformed(ActionEvent ae) {
        cmd = ae.getActionCommand();
        switch(cmd) {
        case "새파일":
            frm.newFile();
            break;

        case "열기":
            JFileChooser opchooser = new JFileChooser();
 
            FileNameExtensionFilter opfilter = new FileNameExtensionFilter("*.txt & *.java","txt","java");
            opchooser.setFileFilter(opfilter);
 
            int ret = opchooser.showOpenDialog(null);
            
            if(ret != JFileChooser.APPROVE_OPTION) {
                JOptionPane.showMessageDialog(null,
                        "파일을 선택하지 않았습니다!!","경고",JOptionPane.WARNING_MESSAGE);
                return;
            }
            
            String oFile = opchooser.getSelectedFile().getPath();
            
            frm.setTitle(oFile);
            frm.loadFile(oFile);
            
            break;

        case "저장":    
            if(frm.getTitle() == "새파일" || frm.getTitle() == "메모장") {
                JFileChooser chooser = new JFileChooser();
 
                FileNameExtensionFilter filter = new FileNameExtensionFilter("*.txt & *.java","txt","java");
                chooser.setFileFilter(filter);
                ret = chooser.showSaveDialog(null);
                
                if(ret != JFileChooser.APPROVE_OPTION) {
                    JOptionPane.showMessageDialog(null,
                            "저장을 취소하셨습니다.","경고",JOptionPane.WARNING_MESSAGE);
                    return;
                }
                
                String sFile = chooser.getSelectedFile().getPath();
                
                frm.setTitle(sFile);
                frm.saveFile(sFile);
            }else {
                frm.setTitle(frm.getTitle());
                frm.saveFile(frm.getTitle());
            }
            break;

        case "다른이름으로 저장":
            if(frm.getTitle() == "새파일" || frm.getTitle() == "메모장") {
                JFileChooser chooser = new JFileChooser();
 
                FileNameExtensionFilter filter = new FileNameExtensionFilter("*.txt & *.java","txt","java");
                chooser.setFileFilter(filter);
                ret = chooser.showSaveDialog(null);
                
                if(ret != JFileChooser.APPROVE_OPTION) {
                    JOptionPane.showMessageDialog(null,
                            "저장을 취소하셨습니다.","경고",JOptionPane.WARNING_MESSAGE);
                    return;
                }
                
                String sFile = chooser.getSelectedFile().getPath();
                
                frm.setTitle(sFile);
                frm.saveFile(sFile);
            }else {
                JFileChooser chooser = new JFileChooser();
 
                FileNameExtensionFilter filter = new FileNameExtensionFilter("*.txt & *.java","txt","java");
                chooser.setFileFilter(filter);
                ret = chooser.showSaveDialog(null);
                
                if(ret != JFileChooser.APPROVE_OPTION) {
                    JOptionPane.showMessageDialog(null,
                            "다른이름으로 저장을 취소하셨습니다.","경고",JOptionPane.WARNING_MESSAGE);
                    return;
                }
                
                String sFile = chooser.getSelectedFile().getPath();
                
                frm.setTitle(sFile);
                frm.saveFile(sFile);
            }
            break;

        case "종료":
            frm.windowClosing(null);
            break;
        }
 
    }
 
}
cs

JFileChooser을 이용해 파일선택 다이어로그를 출력하여 열기, 저장, 다른이름으로 저장 기능을 만들었다. 파일이 선택되면 파일 경로를 Notepad의 메서드의 매개변수로 호출하도록 구성했다.

 

 

Comments