FileInputStream / ByteArrayInputStream의 사용

  

 

package ch4;

 

import java.io.*;

 

public class FileArrayInputStreamTest {

 

       public static void print(InputStream in) {

              byte[] buffer = new byte[512];

              int readcount = 0;

 

              try {

                     while ((readcount = in.read(buffer)) != -1) {

                           System.out.write(buffer, 0, readcount);

                     }

              } catch (IOException e) {

                     System.out.println(e);

              }

       } // print

 

 

 

       public static void main(String[] args) {

              if (args.length != 1) {

                     System.out.println("사용법 : java FileArrayInputStreamTest file/array");

                     System.exit(0);

              } // if end

 

              if (args[0].equals("file")) {

                     FileInputStream fis = null;

                     try {

                           fis = new FileInputStream("file.dat");

                           // static print()메소드 호출

                           print(fis);

                     } catch (Exception ex) {

                           System.out.println(ex);

                     } finally {

                           try {

                                  fis.close();

                           } catch (IOException e) {

                           }

                     }

              } else if (args[0].equals("array")) {

                     byte[] abc = new byte[26];

                     for (int i = 0; i < 26; i++) {

                           abc[i] = (byte) ('a' + i);

                     }

                     ByteArrayInputStream bais = null;

                     try {

                           bais = new ByteArrayInputStream(abc);

                           // static print()메소드 호출

                           print(bais);

                     } catch (Exception ex) {

                           System.out.println(ex);

                     } finally {

                           try {

                                  bais.close();

                           } catch (IOException e) {

                           }

                     }

              } else {

                     System.out.println("사용법 : java FileArrayInputStreamTest file/array");

                     System.exit(0);

              } // if

       } // main

}

 

 

위의 코드에서 static으로 선언된 print 메소드는 인자로서 추상클래스인 InputStream을 받는다. 이것은 InputStream의 하위클래스를 받을수 있다는 의미가 된다.

  

 

파일의 일부분을 조작하고자 하는경우 파일의 내용을 읽어들여 바이트를 배열화 시켜야 할 경우가 생긴다. 다음의 코드는 파일의 내용을 바이트 배열화하고, 바이트 배열을 다시 화면에 출력하는 코드이다.

 

byte abc[i] = (byte) ('a' + i); 의 코드는 integer형을 byte로 캐시팅을 하고있다.

예를 들어

Int (Integer) 형으로 알파벳 ‘a’를 담고 있는 상항이라면 이것은 4 byte의 공간으로 담고 있다는 의미가 된다. 따라서 1 byte 공간의 byte (Byte) 형으로 옮겨 담기위해 캐스팅을 하고 있다.

 

TYPE

SIZE

EXAMPLE

논리형

boolean

1 byte

boolean a = true;

정수형

byte

1 byte

byte a = 127;

short

2 byte

short a = 32767;

int

4 byte

int a = 2147483647;

long

8 byte

long a = -100L;

char

2 byte

char a = 'A';  char b = 65;

실수형

flat

4 byte

float a = 3.14f;

double

8 byte

double a = 2.7d;

double b = 2.7;

 

Posted by Steven J.S Min
,