Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


PushbackReaderÀÇ »ç¿ë¹æ¹ý°ú ¿¹Á¦
 
StreamTokenizerŬ·¡½ºÀÇ ÇÑ°èÀÎ unicode¸¦ Çڵ鸵ÇÒ¼ö ¾ø´Â ¹®Á¦¸¦ ÇØ°áÇÒ¼ö ÀÖ´Â Äڵ带 Á¦½ÃÇغ»´Ù. ( 2003/02/28 ) 102
Written by ienvyou - ÃÖÁö¿õ
1 of 1
 
PushbackReader´Â I/O ÆÐÅ°Áö Ŭ·¡½º·Î¼­ ¹®ÀÚµéÀ» ÀÔ·ÂÀ¸·Î "unread" ½ÃÅ°°Å³ª
"push back"½Ãų ¼ö ÀÖ´Ù.
PushbackReader°´Ã¼°¡ »ý¼ºµÇ¾úÀ»¶§´Â push backÀ» Çã¿ëÇÒ ¹®ÀÚÀÇ °¹¼ö¸¦ 

ÇÁ·Î±×·¥»ó¿¡ Á÷Á¢ ±â¼úÇÏ¿© ÁÖ¾î¾ß ÇÑ´Ù.
ÀϹÝÀûÀ¸·Î ±âº»°ªÀ» 1¹®ÀÚ¸¦ ±âÁØÀ¸·Î ÇÑ´Ù.
¾Æ·¡¿¡ °£´ÜÇÑ ¿¹Á¦¸¦ º¸µµ·Ï ÇÏÀÚ.

            import java.io.*;
            
            public class PushDemo1 {
                public static void main(String args[]) throws Exception {
                    char buf[] = {'t', 'e', 's', 't'};
            
                    // Character¹è¿­·ÎºÎÅÍ reader¸¦ ¼¼ÆýÃŲ´Ù.
            
                    CharArrayReader car = new CharArrayReader(buf);
                    PushbackReader pr = new PushbackReader(car, 1);
            
                    char c;
            
                    // ÇÑ ¹®ÀÚ ÀоîµéÀ̱â.
            
                    c = (char)pr.read();
                    System.out.println(c);
            
                    // ¶Ç ´Ù¸¥ Çѹ®ÀÚ ÀоîµéÀ̱â.
            
                    c = (char)pr.read();
                    System.out.println(c);
            
                    // Çѹ®ÀÚ unread
            
                    pr.unread(c);
            
                    // ´Ù½Ã ÀоîµéÀδÙ.
            
                    c = (char)pr.read();
                    System.out.println(c);
                }
            }


À§ÀÇ Å×½ºÆ® ÇÁ·Î±×·¥Àº PushbackReader¸¦ ÅëÇÏ¿© µÎ°³ÀÇ ¹®ÀÚ("t"¿Í "e")¸¦ 
Àоîµé¿© È­¸é¿¡ »Ñ·ÁÁÖ°í Àִµ¥, ÇÁ·Î±×·¥»ó¿¡¼­ µÎ¹ø°¹®ÀÚÀÎ "e"¿¡¼­ ´Ù½Ã 
push backÇÏ°í ÀÖÀ¸¸ç, read()¸Þ¼Òµå·Î ´Ù½Ã ÀоîµéÀÌ°í ÀÖ´Ù.

°á±¹ È­¸é»ó¿¡ Ãâ·ÂµÇ´Â ³»¿ëÀº ¾Æ·¡¿Í °°´Ù.

        output:
            t
            e
            e

¾Æ·¡¿¡¼­ ´Ù½Ã PushbackReader°´Ã¼¸¦ »ç¿ëÇÏ¿© ´õ ¸¹Àº ¹®ÀÚ¸¦ ÀÌ¿ëÇÑ Àб⸦ ½ÃµµÇغ¸ÀÚ. 
¾Æ·¡ÀÇ ÇÁ·Î±×·¥À» ½ÇÇà½ÃÄÑ º¸¸é Buffer overflow¶ó´Â IOExceptionÀ» ¸¸³ª°Ô µÈ´Ù.

            import java.io.*;
            
            public class PushDemo2 {
                public static void main(String args[]) throws Exception {
                    char buf[] = {'t', 'e', 's', 't'};
            
                    CharArrayReader car = new CharArrayReader(buf);
                    PushbackReader pr = new PushbackReader(car, 1);
            
                    char c1, c2;
            
                    // µÎ°³ÀÇ ¹®ÀÚ¸¦ ÀоîµéÀδÙ.
            
                    c1 = (char)pr.read();
                    System.out.println(c1);
            
                    c2 = (char)pr.read();
                    System.out.println(c2);
            
                    // ¾çÂÊ ¸ðµÎ unread 
            
                    pr.unread(c2);
                    pr.unread(c1);
            
                    c1 = (char)pr.read();
                    System.out.println(c1);
                }
            }
            
À§ÀÇ ÇÁ·Î±×·¥¿¡¼­ ExceptionÀÌ ¹ß»ýÇÏ´Â ÀÌÀ¯´Â µÎ°³ÀÇ ¹®ÀÚ¸¦ pushback½ÃÅ°·ÁÇϱâ 
¶§¹®ÀÌ´Ù. PushbackReader°´Ã¼´Â Çѹ®ÀÚ¸¸À» Áö¿øÇϵµ·Ï ¸¸µé¾îÁ® ÀÖ´Ù.

±×·¯¸é ÀÌ·¯ÇÑ PushbackReader¸¦ ½ÇÁ¦ ÇÁ·Î±×·¥»ó¿¡¼­ ¾î¶»°Ô È°¿ëÇØ¾ß ÇÒÁö ¸·¸·ÇÒ¼öµµ 
ÀÖÀ»°ÍÀÌ´Ù. ÇÑ°¡Áö¸¦ ¿¹·Î µéÀÚ¸é ¸î¸îÀÇ ÀԷ¿¡ ´ëÇÏ¿© tokenÈ­½ÃÄѾßÇÒ °æ¿ìµµ ÀÖÀ»°ÍÀÌ´Ù.
Áï, ´Ü¾îµé°ú ¼ýÀÚµéÀ» ºÐ¸®½Ãų °æ¿ìÀÌ´Ù.

¾Æ·¡¿Í °°Àº ¹®ÀåÀÌ ÀÖÀ»¶§

            testing123abc

ºÙ¾îÀÖ´Â ¹®ÀÚµéÀ» ¾Æ·¡¿Í °°ÀÌ ÀÚ¸£°í ½ÍÀ» °æ¿ìµµ ÀÖÀ» °ÍÀÌ´Ù.

            testing
            123
            abc

¾î¶»°Ô ÀÌÀÏÀ» ó¸®ÇÒ¼ö ÀÖÀ»±î Çѹø¾¿ °í¹Îµé Çغ¸±â ¹Ù¶õ´Ù.
½ÇÁ¦ StringTokenizer³ª StreamTokenizer°°Àº °æ¿ì´Â ÇØ´ç split char¸¦ »ç¿ëÇÔÀ¸·Î¼­
Ç¥ÇöÇÒ¼ö ÀÖ°ÚÁö¸¸ ¹®ÀåÀÌ Çϳª·Î ¿ÏÀüÈ÷ ºÙ¾îÀÖ´Â °æ¿ì¶ó¸é ±×·± Äڵ带 Àû¿ëÇϱ⵵ Èûµé °ÍÀÌ´Ù.

PushbackReader¸¦ »ç¿ëÇÑ´Ù¸é À§ÀÇ ¹®ÀåÀ» Çϳª¾¿ À߶󳾼ö ÀÖ´Ù.
¸ÕÀú µé¾î¿À´Â ÀÔ·ÂÀ» ÀÌ¿ëÇÏ¿© Çϳª¾¿ read()Çϸ鼭 ¹®ÀÚ¸¦ Àо¾î
ÇØ´ç ¹®ÀÚ°¡ digitÀÎÁö letterÀÎÁö¸¦ ºÐ¸®½Ãų¼ö°¡ ÀÖÀ» °ÍÀÌ°í, ¸¸¾à ´Ü¾î(letterÀÇ Á¶ÇÕ)°¡ ³¡³ª°í 
¼ýÀÚ°¡ ½ÃÀÛÀÌ µÈ´Ù¸é ÇØ´ç digitÀ» ¸¸³ª´Â ºÎºÐ¿¡¼­ unread()·Î pushbackÀ» ½ÃŲ ÈÄ 
´Ù½Ã read¸¦ ÇÏ¸é µÇ±â ¶§¹®ÀÌ´Ù.

±×¿¡ ´ëÇÑ »ùÇÃÄڵ带 ¾Æ·¡¿¡¼­ º¸µµ·Ï ÇÏÀÚ.

            import java.io.*;
            
            class ParseInput {
                private PushbackReader pr;

                // types of tokens
                private static final int NONE = 0;
                private static final int LETTER = 1;
                private static final int DIGIT = 2;
            
                // ÆÄÀÏÀ» À§ÇÑ PushbackReader ¸¦ ¼¼ÆÃÇÑ´Ù.
            
                public ParseInput(String fn) throws IOException {
                    FileReader fr = new FileReader(fn);
                    BufferedReader br = new BufferedReader(fr);
                    pr = new PushbackReader(br, 1);
                }
            
                // ÆÄÀϷκÎÅÍ ´ÙÀ½ ÅäÅ«À» ¾ò¾î³½´Ù.
            
                public String getToken() throws IOException {
                    int c;
                    char ch;
                    int type = NONE;
            
                    // ù¹ø° ¹®ÀÚ¸¦ ã¾Æ³½´Ù.
            
                    do {
                        c = pr.read();
            
                        // ÆÄÀÏÀÇ ¸¶Áö¸·ÀÎÁö üũÇÑ´Ù.
            
                        if (c == -1) {
                            return null;
                        }
                        ch = (char)c;
            
                        // ¼ýÀÚÀÎÁö ¹®ÀÚÀÎÁö ºÐ¸®Çس½´Ù.
            
                        if (Character.isLetter(ch)) {
                            type = LETTER;
                        }
                        else if (Character.isDigit(ch)) {
                            type = DIGIT;
                        }
                    } while (type == NONE);
            
                    // StringBuffer¿¡ ÃÖ¼Ò Çѹ®ÀÚ¸¦ ¼¼ÆÃÇÒ¼ö ÀÖ´Ù.
            
                    StringBuffer sb = new StringBuffer();
                    sb.append(ch);
            
                    for (;;) {
                        c = pr.read();
                        if (c == -1) {
                            break;
                        }
                        ch = (char)c;
            
                        // ¸ÕÀú µé¾î¿Â ¹®ÀÚ¿Í °°Àº ŸÀÔÀÇ char¶ó¸é ¹öÆÛ¿¡ Ãß°¡ÇÑ´Ù.
                // ¾Æ´Ò °æ¿ì unread()¸Þ¼Òµå¸¦ ÅëÇÏ¿© pushback½ÃŲ´Ù.
            
                        if ((type == LETTER && Character.isLetter(ch)) || 
                            (type == DIGIT && Character.isDigit(ch))) {
                            sb.append(ch);
                        }
                        else {
                            pr.unread(c);
                            break;
                        }
                    }
            
                    return sb.toString();
                }
            
                public void close() throws IOException {
                    pr.close();
                }
            }
            
            public class PushDemo3 {
                public static void main(String args[]) throws IOException {
            
                    if (args.length != 1) {
                        System.err.println("Missing input file");
                        System.exit(1);
                    }
            
                    ParseInput pi = new ParseInput(args[0]);
                    String token;
            
                    // ÀÔ·ÂÆÄÀϷκÎÅÍ ÅäÅ«À» ¾ò¾î³»µµ·Ï ÇÑ´Ù.
            
                    while ((token = pi.getToken()) != null) {
                        System.out.println(token);
                    }
            
                    pi.close();
                }
            }

À§ÀÇ ÇÁ·Î±×·¥Àº ÀоîµéÀÎ ÆÄÀϷκÎÅÍ µ¥ÀÌÅ͸¦ ÀÔ·Â¹Þ¾Æ ¹®ÀÚ¿Í ¼ýÀÚ¸¦ ºÐ¸®ÇÏ¿© 
ó¸®Çس»´Â ÄÚµåÀÌ´Ù. 
ÁÖ¼®À» ¸ðµÎ ´Þ¾Æ³õ¾ÒÀ¸¹Ç·Î ÃæºÐÈ÷ ÀÌÇØ°¡ °¡¸®¶ó º»´Ù.
¸¸¾à ÆÄÀÏ¿¡¼­ ¾Æ·¡¿Í °°Àº ³»¿ëÀÌ ÀԷµǾú´Ù°í °¡Á¤ÇÏÀÚ.

            testing123this4is56a78test9

Ãâ·ÂÀº ¾Æ·¡¿Í °°Àº ÇüÅ·Π³ª¿À°Ô µÈ´Ù.

            testing
            123
            this
            4
            is
            56
            a
            78
            test
            9

ÀÚ¹ÙAPI¿¡¼­´Â StreamTokenizerŬ·¡½º°¡ ÀÖ±â´Â ÇÏÁö¸¸ ÀÌ Å¬·¡½º´Â ¹ÙÀÌÆ®±â¹ÝÀÇ 
½ºÆ®¸²À̱⠶§¹®¿¡ unicode¹®ÀÚ¿¡ ´ëÇÏ¿© ÅäÅ«À» ±¸¼ºÇϱâ´Â ¾à°£ÀÇ ¹®Á¦°¡ ÀÖ´Ù.
ÀÌ·²°æ¿ì PushbackReader¸¦ »ç¿ëÇÔÀ¸·Î½á ±×·¯ÇÑ ¹®Á¦Á¡µéÀ» ÇØ°áÇس¾¼ö ÀÖ´Ù.
 
1
References
 
http://java.sun.com/j2se
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD