본문 바로가기

Programming/Java

DataInputStream / DataOutputStream

반응형

http://twinw.tistory.com/96 자바파일입출력 정리


DataOutputStream


DataOutputStream은 자바의 기본 데이터 타입별로 출력하는 메소드가 존재한다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.io.*;
 
public class DataOutputStreamEx {
    public static void main(String[] args){
        FileOutputStream f = null;
        DataOutputStream out = null;
         
         try {
            f = new FileOutputStream("C:\\Users\\han\\Desktop\\test.txt");
            out = new DataOutputStream(f);
            out.writeBoolean(false); // 0x00 출력
            out.writeByte(0x33); // 0x33 출력
            out.writeShort(0x1234); // 0x1234 출력
            out.writeInt(0x5678); // 0x00005678 출력
            out.writeLong(0x12abcdef); // 0x0000000012abcdef 출력
            out.writeChar('c'); // 0x0063 출력
            out.writeDouble(0.12e-3); // long으로 변환 후 0x3f1f75104d551d69 출력
            out.writeFloat((float3.14); // int로 변환 후 0x4048f5c3 출력
            out.writeUTF("가나다라"); // UTF-8로 인코딩 후 0x000ceab080eb8298eb8ba4eb9dbc 출력
            out.close();
            f.close();
        }
        catch (IOException e) {
            System.out.println("입출력 오류");
        }
    }
}
cs




DataInputStream


바이너리 형태로 입력했던 자바 기본 데이터타입 데이터를 DataInputStream을 이용해서 다시 읽어올 수 있다.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class ReadData {
    public static void main(String[] args){
        FileInputStream f = null;
        DataInputStream in = null;
        try
        {
            f = new FileInputStream("C:\\Users\\han\\Desktop\\test.txt"); 
            in = new DataInputStream(f); // 파일에 연결된 데이터 입력 스트림 생성
            System.out.println(in.readBoolean()); // 1바이트 읽어 boolean값 반환
            System.out.printf("%x\n",(byte)in.readByte()); // 1바이트 읽어 byte값 반환
            System.out.printf("%x\n",in.readShort()); // 2바이트 읽어 short값 반환
            System.out.printf("%x\n",in.readInt()); // 4바이트 읽어 int값 반환
            System.out.printf("%x\n",in.readLong()); // 8바이트 읽어 long값 반환
            System.out.println((char)in.readChar()); // 2바이트 읽어 char값 반환
            System.out.println(in.readDouble()); // 8바이트 읽어 double값 반환
            System.out.println(in.readFloat()); // 4바이트 읽어 float값 반환
            System.out.println(in.readUTF()); // UTF-8로 인코딩된 문자열 읽어 반환
            in.close();
            f.close();
        }
        catch (IOException e)
        {
            System.out.println("입출력 오류");
        }
    }
}
 
cs









---------------------------------------------------------------------------------------



// 인코딩 문제 발생

wr.writeBytes("Content-Disposition: form-data; name=\"title\"\r\n\r\n" + doc_title);


// 에러 발생

wr.writeUTF("Content-Disposition: form-data; name=\"title\"\r\n\r\n" + doc_title);



// 문제점 해결

wr.writeBytes("Content-Disposition: form-data; name=\"title\"\r\n\r\n");

wr.writeUTF(doc_title);




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
try {
                 URL url = new URL("https://openapi.naver.com/blog/writePost.json");
                URLConnection connection = url.openConnection();
                connection.setRequestProperty("Accept-Charset""utf-8");
                connection.setRequestProperty("Authorization", token_type + " "+ access_token);
                connection.setRequestProperty("X-Naver-Client-Id", client_id);
                connection.setRequestProperty("X-Naver-Client-Secret", client_secret);
                connection.setRequestProperty("Content-Type""multipart/form-data; boundary="+boundary);
                connection.setDoOutput(true);
                connection.setDoInput(true);
                
                // 문자열 보내기
                DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
                wr.writeBytes("\r\n--" + boundary + "\r\n");
                wr.writeBytes("Content-Disposition: form-data; name=\"title\"\r\n\r\n");
                wr.writeUTF(doc_title);
                wr.writeBytes("\r\n--" + boundary + "\r\n");
                wr.writeBytes("Content-Disposition: form-data; name=\"contents\"\r\n\r\n");
                wr.writeUTF(doc_content);          
                wr.writeBytes("\r\n--" + boundary + "--\r\n");
                wr.flush();
                
            } catch (Exception e) {
                e.printStackTrace();
            }
        } finally {
            wr.close();
        } 
cs




반응형