군포망나니 2025. 6. 18. 22:24
반응형

 

더보기

CHAPTER 01 바이트 입출력 스트림 

CHAPTER 02 필터 입출력 스트림 

CHAPTER 03 파일처리 클래스 

CHAPTER 04 문자 입출력 스트림 

CHAPTER 05 인터넷 주소 처리 

CHAPTER 06 URL 클래스를 이용하여 데이터 읽기 

CHAPTER 07 TCP/IP 서버 소켓 

CHAPTER 08 TCP/IP 클라이언트 소켓 

CHAPTER 09 서버와 클라이언트 통신 프로그램 

CHAPTER 10 채팅 프로그램 작성 

CHAPTER 11 UDP 프로토콜 

CHAPTER 12 URLConnection 클래스 

CHAPTER 13 IP 멀팈캐스팅 소켓 

 

 

드이어 CHAPTER 01 이 끝나고 연습문제만 남았습니다!! 한 번 고민고민해보면서 풀어봅시당 

 

다음 예제의 마지막 수행문인 write(b) 메소드를 write(byte[] data, int offset, int length) 메소드를 사용해서 같은 결과를 가지도록 수정하시오. 

public class DisplayCharacterBlock {

    public static void dcb() throws Exception {
        byte[] b = new byte[(172 - 31) * 2];
        int index = 0;

        for(int i = 32; i<127; i++){
            b[index] = (byte) i;
            if(i%8 == 7)
                b[index+1] = (byte)'\n';

            else
                b[index++] = (byte)'\t';
        }

        b[index++] = (byte)'\n';
        System.out.write(b);

    }
}

 

 

음 예제의 arraycopy() 메소드를 별도의 함수로 직접 작성해서 사용하시오. 

static int bufferSize = 80; 
static int size = 0; 
static byte buffer[] = new byte[bufferSize];
	 

	 public static void main(String args[]) throws IOException {
		 try{
			 	int dataRead;
				 while((dataRead = System.in.read(buffer, size, bufferSize-size)) >= 0) {
				 size += dataRead; 
				 if(size == bufferSize) 
					 increaseBufferSize();
			 }
			 System.out.write(buffer, 0, size);
		 } catch(IOException e) {
			 System.err.println("스트림으로부터 데이터를 읽을 수 없습니다.");
		 }
	 }
	 
	 static void increaseBufferSize(){
		 bufferSize += 80; 
		 byte[] newBuffer = new byte[bufferSize];
		 System.arraycopy(buffer,  0,  newBuffer,  0,  size);
		 buffer = newBuffer; 
	 }

 

 

 

다음 예제의 "저장" 버튼 옆에 "닫기" 버튼을 추가해서 이 버튼을 클릭하면 윈도우가 사라지도록 하시오. 

import java.awt.*;
import java.awt.event.*;
import java.io.*;

class WriteToFileEvent extends Frame implements ActionListener {
    Label lfile, ldata;
    TextField tfile, tdata;
    Button save;
    String filename, data;
    byte[] buffer = new byte[80];

    public WriteToFileEvent(String str) {
        super(str);
        setLayout(new FlowLayout());

        lfile = new Label("파일이름을 입력하세요.");
        add(lfile);

        tfile = new TextField(20);
        add(tfile);

        ldata = new Label("데이터를 입력하세요.");
        add(ldata);

        tdata = new TextField(20);
        add(tdata);

        save = new Button("저장하기");
        save.addActionListener(this);
        add(save);
        System.out.print("save :::" + save);

        addWindowListener(new WinListener());
    }

    public static void main(String[] args) {
        WriteToFileEvent text = new WriteToFileEvent("파일저장");
        text.setSize(270, 180);
        text.show();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        filename = tfile.getText();
        data = tdata.getText();
        System.out.print("data :::" + data);

        buffer = data.getBytes();
        System.out.print("buffer :::" + buffer);


        try{    // 현재 루트(상대경로) 일 때 
           FileOutputStream fout = new FileOutputStream(filename);
           // 절대경로일 때 : /Users/사용자이름/Documents/"
           fout.write(buffer);
        } catch (Exception ex){
            System.out.println(ex.getMessage());
        }
    }

    static class WinListener extends WindowAdapter {
        public void windowClosing(WindowEvent e){
            System.exit(0);
        }
    }
}

 

 

 

키보드로부터 입력한 문자열을 특정한 파일에 저장하고 저장된 파일의 내용을 읽어 화면에 출력하는 클래스를 작성하시오. 

import java.io.*;
public class InOutFile {

	public static void main(String args[]){ 
		
        //여기다가 작성하시오. 
        
	}
}

 

 

다음 포스팅은 CHAPTER 02 | 필터 입출력 스트림 클래스에 대해서 작성해보겠습니다 !!! 

반응형