개발

텍스트 문서 파싱하기

에드몽단테스 2007. 9. 15. 14:38
구분자가 '=' 로 되어있는 텍스트파일을 key값과 value로 파싱한다고 하자.
그럼 우선, 파일을 라인단위로 읽어서 각각의 라인을 '=' 로 구분하여 key값과 value값을 구별해낸다.
텍스트 문서가 한글로 되어 있다면 한글이 깨지지 않도록 InputStram 으로 읽지 않고 FileReader 로 읽으면 문제될 것도 없어 보인다.

private void makeFile(File file, File referenceFile)
    {
        FileWriter fw = null;
        BufferedReader br = null;
        String key = "", value = "", line = "";
        int index = -1;
       
        try {
             fw = new FileWriter("new_"+referenceFile.getName());
             br = new BufferedReader(new FileReader(file));
            
             while((line = br.readLine()) != null)
             {
                 if(line.trim().length() == 0 || line.charAt(0) == '#')
                     fw.write(line+"\n");
                 else
                 {
                     if((index = line.indexOf('=')) != -1)
                     {
                         key = line.substring(0, index);
                         value = getValue(key) == null ? line.substring(index+1) : getValue(key);
                         fw.write(key+"="+value+"\n");
                     }
                 }
             }
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex){
            ex.printStackTrace();
        } finally{
            if(fw != null)try{fw.close();}catch(Exception e){}
            if(br != null)try{br.close();}catch(Exception e){}
        }
    }

하지만 이렇게 해도 문제는 있다.
한글내용도 잘 파싱이 되지만, 한글이 UTF-8이면 한글이 깨진채로 파싱이된다.
그러므로 BufferedReader의 readLine()함수로는 한글을 정상적으로 읽을 수 없다.


파일을 바이트배열로 읽어보자.
private void makeFile(File file, File referenceFile, boolean bUTF8)
    {
        String key = "", value = "", line = null;
        int index = -1, red = -1;
        FileInputStream fis = null;
        FileOutputStream fos = null;
        ByteArrayOutputStream baos = null;
       
        try {
             fis = new FileInputStream(file);
             fos = new FileOutputStream("new_"+referenceFile.getName());
             baos = new ByteArrayOutputStream();
            
             while((red = fis.read()) != -1)
             {
                 if(red == '\n')
                 {
                     byte[] bytes = baos.toByteArray();
                     baos.reset();
                    
                     if(bUTF8)
                         line = new String(bytes, "UTF-8");
                     else
                         line = new String(bytes);
                    
                     if(line.trim().length() == 0 || line.charAt(0) == '#')
                     {
                         if(bUTF8)
                             fos.write((line+"\n").getBytes("UTF-8"));
                         else
                             fos.write((line+"\n").getBytes());
                     }
                     else
                     {
                         if((index = line.indexOf('=')) != -1)
                         {
                             key = line.substring(0, index);
                             value = getValue(key) == null ? line.substring(index+1) : getValue(key);
                            
                             if(bUTF8)
                                 fos.write((key+"="+value+"\n").getBytes("UTF-8"));
                             else
                                 fos.write((key+"="+value+"\n").getBytes());
                         }
                     }
                 }
                 else
                 {
                     baos.write(red);
                 }
             }
            
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex){
            ex.printStackTrace();
        } finally{
            if(baos != null)try{baos.close();}catch(Exception e){}
            if(fos != null)try{fos.close();}catch(Exception e){}
            if(fis != null)try{fis.close();}catch(Exception e){}
        }
    }

바이트배열로 파일을 읽어들인 후 UTF-8여부를 판단하여 String 을 만들면 정상적으로 파싱할 수 있다.

반응형