[ I/O 기초 ] I/O 기초 및 실습

2009. 6. 23. 17:57 - 에릭투스

1. NodeStream : 수도꼭지 , 필터되지 않은 원 자제

  1) InputStream
    * System.in : 키보드의 입력(os에 따라 다르다)
      메소드 : read()
         read(byte[]b), read(byte[]b, 시작, 종료) 0,length
   
  2) OutputStream
     write(),write(byte[]b),
     write(byte[]b,시작,종료)  0,n (읽어들인 바이트 수)
  3) FileInputStream
  4) FileOutputStream
  5) FileReader 2byte
  7) FileWriter 2byte


2. FilterStream :  정수기에 비유, 사용하기 편하게 필터됨.
   * NodeStream 보다 효율적이고 빠르다, 다양한 Method 지원.
   * 반드시 NodeStream이 필요하다.
   so Constructor Type
     1. FilterStream(NodeStream) : 감싸는 경우.

 FilterStream의 종류

 (1) Buffer 계열
    : 미리 공간을 잡아놓고 저장했다가 , 내보냈다가,(계란판)
    : 실행이 빠르다.

    1) BufferedInputStream
    2) BufferedOutputStream

  (2) Data 계열. : 기본형 데이터 & Unicode String을 읽거나 쓴다.
       : Primative variable & Unicode String
        * Unicode : code에 제한받지 않고 자유로운 아이~
       : so , os,language 에 구애받지 않고 모두 표현 할 수 있다.
 
  ps.  메신저는 java가 없어요~(만들수는 있지만 권장하지 않음)
        <이거는아마도 네트웍 개념인듯....내생각..>
 
    3) DataInputStream
    4) DataOutputStream ( flush(); 필수 )

   (3) Buffered 계열 : Buffer방식을 기본으로 채택하고 있고 좀더 유용한
      : String관련 메소드를 제공.

    5) BufferedReader
    6) BufferedWriter ( flush(); 필수 )
   
    7) PrintWriter : Bridge Stream이 필요없다. , flush() 가 필요없다, Method가 다양함.
* General PrintStream 은 반드시 flush() 가 필요하지만 PrintWriter 는 필요가 없다.
    ex) System.out.println

3. Bridge Stream
 1byte Stream 을 2byte Stream으로 변환해주는 Stream
 = byte 로 읽고 쓰는 것을 char 으로 읽고 쓰게끔 변환

   1) InputStreamReader : InputStream 을 Reader  로 바꿔주는 역할
   2) OutputStreamWriter : OutputStream 을 Writer 로 바꿔줌.

4. File Class
   1) 파일과 디렉토리(폴더)를 객체로 만들기위해 설계된 클래스.
   
   2) 생성자
     1> File(File parent, String child)
     2> File(String pathname)
     3> File(String parent, String chiild)
     4> File(URI uri)
   3) Method
     1> exist() : 존재하는지 검사 (return boolean: 있으면 true, 없으면 false)
     2> getName()
     3> getPath()
     4> getParent()
     5> list()
     6> isDirectory(), isFile(),
       canRead(),
       canWrite()
     7> mkdir() :

 
 ------------------------------------------------------------------------------------------------------------------------------------
 실습(1) : FileOutputStream / DataOutputStream
 -----------------------------------------------------------------------------------------------------------------------------------
  FileOutputStream (" 경로 " );
  DataOutputStream ( FileOutputStream );


------------------------------------------------------------------------------------------------------------------------------------
 실습(1) : FileInputStream / DataInputStream
-----------------------------------------------------------------------------------------------------------------------------------
  FileInputStream (" 경로 ");
  DataInputStream ( FileInputStream );

* 읽어들일때 write 한 순서 까지 맞춰줘야한다. (어쩔수 없는 특징)

------------------------------------------------------------------------------------------------------------------------------------
 실습(2) : File
-----------------------------------------------------------------------------------------------------------------------------------
 Constructor
  File(String pathname);

 Method
  exists(); (return boolean); (존재시 true / 없을경우 false)
  isFile() : (return boolean); (File일때 true / 아닐경우 false)
  isDirectory() : (return boolean);

/*
 c:/ : 절대경로
 c:  : 실행하는 디렉토리 경로. (현재 실행하는 곳)
 src : 상대경로
 c:/wan/io/04/src 절대경로.

 String []list = f.list();
 for(String s : list){
   System.out.println(s);
 }
File.listRoots() (return File[])
*/


-----------------------------------------------------------------------------------------------------------------------------------
 실습(2) : File Tree
-----------------------------------------------------------------------------------------------------------------------------------
 ex>
 File [] root = File.listRoots();
 //결과 : 기본적으로 윈도우에서는 내컴퓨터(Root)로 감.
JFrame frame = new JFrame("파일트리");
JTree tree = new JTree(root);
frame.add(tree,"Center");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);

\
-----------------------------------------------------------------------------------------------------------------------------------
 실습(2) : 계속 들어가기
-----------------------------------------------------------------------------------------------------------------------------------

for(File file : root){
 if(file.isDirectory()){
  String[] dirList = file.list();
   for(String str : dirList){
     System.out.println(str);
   }
 }else{
  System.out.println(file.getName());
 }
}

-----------------------------------------------------------------------------------------------------------------------------------
 실습(3) TestFile02 : Directory File 만들기
-----------------------------------------------------------------------------------------------------------------------------------

1>File f = new File(String parent , String child);

  Directory create Method
    1) File . mkdir(); : mkdir은 부모가 존재하지 않으면 만들어주지 않는다.
    2) File . mkdirs(); : mkdirs 부모의 존재유무에 상관없이 무조건 만든다.

  * 상황에 따라 선택사용
   부모가 없을때 만들어지면 안되는 경우 (ex) update의 경우)
   무조건 폴더 구조 (ex Install 할때)가 필요한 경우 mkdirs

2> File(File parent, String child)

실습에 사용한 File 의 Method들
   File . getParent() (return String) : 부모 file return
   File . getPath() (return String) : 경로
   File . getAbsolutePath() (return String) : 절대경로


---------------------------------------------------------------------------------------------------------------------------------------------
www.thewari

'자바 기본 공부 > I/O' 카테고리의 다른 글

[ I/O 기초 ] I/O 기초 실습  (0) 2009.06.23
[ IO 기초 ] I/O 기초이론  (0) 2009.06.23

다른 카테고리의 글 목록

자바 기본 공부/I/O 카테고리의 포스트를 톺아봅니다