예제 9월 2일 FileInputStream 객체를 이용한 텍스트 뷰어
2009. 9. 2. 15:12
package net.jeongsam.gui; import java.awt.*; import java.awt.event.*; import java.io.*; @SuppressWarnings("serial") class MyViewer extends Frame implements ActionListener { private TextField txtFileName; private Button btnOpen; private TextArea txtTextArea; public MyViewer() { super("텍스트 뷰어"); txtFileName = new TextField(20); btnOpen = new Button("열기"); txtTextArea = new TextArea(20, 20); btnOpen.addActionListener(this); Panel p = new Panel(); p.add(new Label("파일명:")); p.add(txtFileName); p.add(btnOpen); add(p, BorderLayout.NORTH); add(txtTextArea, BorderLayout.CENTER); setSize(400, 300); setVisible(true); } public static void main(String[] args) { new MyViewer(); } public void actionPerformed(ActionEvent e) { try { int rByte = 0; FileInputStream fis = new FileInputStream(txtFileName.getText()); while ((rByte = fis.read()) != -1) { txtTextArea.setText(txtTextArea.getText() + (char)rByte); } fis.close(); } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } } }