티스토리 뷰

[java]파일 읽고, 쓰기, 복사, 삭제 종합편


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.LineNumberReader;

public class first {
    void Rinfor() {
    try{
        String wantStr = "";
        String tempStr = "";
        BufferedReader br = new BufferedReader(new FileReader("D:/JSP/Project/Test/out/test5030AB1001.20100609.165119.out"));
        LineNumberReader lr = new LineNumberReader(br);
        //FileWriter fw = new FileWriter("D:/JSP/Project/Test/out_logs/test5030AB1001.20100609.165119.out");
        //FileOutputStream os = new FileOutputStream("D:/JSP/Project/Test/out_logs/test5030AB1001.20100609.165119.out"); //수정파일
        BufferedWriter bw = new BufferedWriter(new FileWriter("D:/JSP/Project/Test/out_logs/test5030AB1001.20100609.165119.out"));
        while((tempStr = lr.readLine()) != null){
            bw.write(tempStr+"\n");
            wantStr = tempStr.substring(0, 2);    //자료구분 2자리
            //fw.write(tempStr);
            //os.write(tempStr);
            //System.out.println(wantStr);
            if(wantStr.equals("BD")){
                //AuthCode값 구하기
                wantStr = tempStr.substring(139, 151);
                System.out.println("제휴사승인번호-> [ "+wantStr+" ]");
                //PG_TID 구하기
                wantStr = tempStr.substring(291, 299);
                System.out.println("승인일자-> [ "+wantStr+" ]");
                //PG_TID 구하기
                wantStr = tempStr.substring(305, 314);
                System.out.println("승인번호-> [ "+wantStr+" ]");
                //RST_CODE 구하기
                wantStr = tempStr.substring(353, 357);
                System.out.println("응답코드 대분류-> [ "+wantStr+" ]");
                //RST_CODE 구하기
                //wantStr = tempStr.substring(355, 357);
                //System.out.println("응답코드 소분류-> [ "+wantStr+" ]");
                //요청메세지 구하기
                wantStr = tempStr.substring(357, 385);
                System.out.println("Message1 -> [ "+wantStr.trim()+" ]");
            }
           
            bw.flush();
        }
        //fw.close();
        br.close();
        lr.close();
        //File f = new File("D:/JSP/Project/Test/out/test5030AB1001.20100609.165119.out");
        //f.delete();
       
    }catch(IOException e){
        System.err.println(e);
        System.exit(1);
    }
    }
   
    public static void main(String args[]){

        first rp = new first();
        rp.Rinfor();
                               
    }   
}