티스토리 뷰

반응형
IOEx1
import java.io.*;
import java.util.Arrays;

class IOEx1 
{
	public static void main(String[] args) 
	{
		byte[] inSrc = {0,1,2,3,4,5,6,7,8,9};
		byte[] outSrc = null;

		ByteArrayInputStream input = null;
		ByteArrayOutputStream output = null;

		input = new ByteArrayInputStream(inSrc);
		output = new ByteArrayOutputStream();

		int data = 0;

		while((data = input.read())!=-1) {
			output.write(data);	// void write(int b)
		}

		outSrc = output.toByteArray();

		System.out.println("Input Source  :" + Arrays.toString(inSrc));
		System.out.println("Output Source :" + Arrays.toString(outSrc));
	}
}
결과
Input Source  :[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Output Source :[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]



IOEx2
import java.io.*;
import java.util.Arrays;

class IOEx2
{
	public static void main(String[] args) 
	{
		byte[] inSrc = {0,1,2,3,4,5,6,7,8,9};
		byte[] outSrc = null;

		byte[] temp = new byte[10];

		ByteArrayInputStream input = null;
		ByteArrayOutputStream output = null;

		input = new ByteArrayInputStream(inSrc);
		output = new ByteArrayOutputStream();

		input.read(temp,0,temp.length); // 읽어 온 데이터를 배열 temp에 담는다.
		output.write(temp,5, 5);	    // temp[5]부터 5개의 데이터를 write한다.
		// while문을 사용하지 않음
	
		outSrc = output.toByteArray();

		System.out.println("Input Source  :" + Arrays.toString(inSrc));
		System.out.println("temp              :" + Arrays.toString(temp));
		System.out.println("Output Source :" + Arrays.toString(outSrc));

	}
}

결과

Input Source  :[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

temp              :[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Output Source :[5, 6, 7, 8, 9]




IOEx3

import java.io.*;
import java.util.Arrays;

class IOEx3 {
	public static void main(String[] args) {
		byte[] inSrc = {0,1,2,3,4,5,6,7,8,9};
		byte[] outSrc = null;

		byte[] temp = new byte[4];	// 이전 예제와 배열의 크기가 다르다.

		ByteArrayInputStream input = null;
		ByteArrayOutputStream output = null;

		input = new ByteArrayInputStream(inSrc);
		output = new ByteArrayOutputStream();

		//
		try {
			while(input.available() > 0) {
				input.read(temp);
				output.write(temp); 
//                      	System.out.println("temp :" + Arrays.toString(temp));
			}
		} catch(IOException e) {} // 
		
		outSrc = output.toByteArray();

		System.out.println("Input Source  :" + Arrays.toString(inSrc));
		System.out.println("temp          :" + Arrays.toString(temp));
		System.out.println("Output Source :" + Arrays.toString(outSrc));
	}
}
 
결과
Input Source  :[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
temp          :[8, 9, 6, 7]
Output Source :[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 6, 7]



IOEx4
import java.io.*;
import java.util.Arrays;	// JDK1.5부터 추가된 클래스이다.

class IOEx4
{
	public static void main(String[] args) 
	{
		byte[] inSrc = {0,1,2,3,4,5,6,7,8,9};
		byte[] outSrc = null;

		byte[] temp = new byte[4];

		ByteArrayInputStream input = null;
		ByteArrayOutputStream output = null;

		input = new ByteArrayInputStream(inSrc);
		output = new ByteArrayOutputStream();

		try {
			while(input.available() > 0) {
				int len = input.read(temp); // 읽어 온 데이터의 개수를 반환한다.
				output.write(temp, 0, len); // 읽어 온 만큼만 write한다.
			}
		} catch(IOException e) {}

		outSrc = output.toByteArray();

		System.out.println("Input Source  :" + Arrays.toString(inSrc));
		System.out.println("temp          :" + Arrays.toString(temp));
		System.out.println("Output Source :" + Arrays.toString(outSrc));
	}
}

결과

Input Source  :[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

temp          :[8, 9, 6, 7]

Output Source :[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]



반응형
댓글
반응형
최근에 올라온 글
최근에 달린 댓글
«   2024/05   »
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