정보보안공부

Java2_day12 본문

Language/Java2

Java2_day12

Steady_sp 2017. 3. 25. 01:08

***n01.BufferedStream - FileSplitEx

 

 

-> FileInputStream fis = new FileInputStream(fname);
    BufferedInputStream bis = new BufferedInputStream(fis); 선언

    보조스트림, 파일입력 스트림에서 데이터를 모으기 위한 기능

-> final int VOLUME = 1 * 1024         //1KB단위

    String fname = "C:/Temp2/aaa";     //만들어져있는 파일

-> FileOutputStream fos = null;

    BufferedOutputStream bos = null;

-> int data; int i=0; int number = 0;

 

-> while((data = bis.read()) != -1) { //aaa파일을 읽는다. //bis = fis

    if(i%VOLUME) { if(i != 0) { bos.close(); }

    fos = new FileOutputStream(fname + "_" + ++number);

    bos = new BufferedOutputStream(fos); }

    bos.write(data); //쪼개진 파일에 글자작성 //주석처리시 파일에 아무것도 없다.

    i++; }

    bis.close();

    bos.close();

-> buffered를 활용하긴 했지만 buffered안해도 fis, fos만으로도 가능한 클래스이다

 

*** n01.BufferedStream - FileMerge

 

 

-> String mergeFile = "C:/Temp2/bbb";     //결과 파일
    String splitFile = "C:/Temp2/aaa";         //원래있던 파일

 

-> File temp = File.createTempFile("C:/Temp2/temp",".tmp");     //파일위치,확장명

 

-> FileOutputStream fos = new FileOutputStream(temp);
    BufferedOutputStream bos = new BufferedOutputStream(fos);

-> FileInputStream fis = null;
    BufferedInputStream bis = null;

 

-> int number = 1; File f = new File(splitFile + "_" + number);

-> while(f.exists() {     // f파일이 존재할때 반복문

    f.setReadOnly();    // 작업중에 파일의 내용이 변경되지 않도록 한다.

    bis = new BufferedInputStream(new FileInputStream(f));

    //bis = f파일을 버퍼 입력 스트림

 

    int data;

    while((data=bis.read()) != -1) { //f파일을 읽고 data에 저장

    bos.write(data);        //data를 쓴다 bos 버퍼출력스트림에 저장

 

    bis.close();        //f파일 다읽으면 창닫기

    number++;        //number + 1

    f = new File(splitFile + "_" + number);

    }

    bos.close();

    File old = new File(mergeFile);        //bbb파일이 존재하는지 확인

    if(old.exists()) old.delete();                //존재하면 삭제

    temp.renameTo(old);                        //temp파일명을 old(bbb)로 수정

 

*** n01.BufferedStream - FileKeyinMerge

 

 

 

*** n02.DataInputStream - DataOutputStream

 

 

-> FileOutputStream fout = new FileOutputStream("C:/Temp2/math.dat");
    DataOutputStream dout = new DataOutputStream(fout);

-> dout.writeInt(3);        //4바이트 정수형식으로 저장

    dout.writeDouble(Math.PI);        //3.1415... 8바이트실수 형식으로 저장

    dout.writeUTF("가나다abc");       //UTF 인코딩, 문자열저장

    dout.writeChars("가나다abc");    //char형 문자 6개로 저장

 

-> dout.close(); fout.close();

-> 실행하면 파일이 만들어진다.

 

-> FileInputStream fin = new FileInputStream("C:/Temp2/math.dat");
    DataInputStream din = new DataInputStream(fin);

-> int a = din.readInt();
    double b = din.readDouble();
    String d = din.readUTF();

 

-> System.out.println(a);
    System.out.println(b);
    System.out.println(d);
    for(int i=0;i<6;i++)
        System.out.println(din.readChar());
    System.out.println();

 

-> din.close(); fin.close();

-> 실행하면 파일을 읽어들어온다.

'Language > Java2' 카테고리의 다른 글

Java2_day11  (0) 2017.03.24
Java2_day09  (0) 2017.03.22
Java2_day08  (0) 2017.03.21
Java2_day07  (0) 2017.03.18
Java2_day06  (0) 2017.03.17
Comments