JAVA

Java IO 4 - DataInputStream, DataOutputStream

devstep88 2023. 8. 24. 16:56

DataInputStream, DataOutputStream

기본형 타입과 문자열을 읽고 쓸 수 있다.

public class IOExam10 {
    public static void main(String[] args) throws Exception{
        // 문제 이름, 국어, 영어, 수학, 총첨 평균점수를 /tmp/score.dat 파일에 저장하시오
        String name ="choi";
        int kor = 90;
        int eng = 80;
        int math = 85;
        double total = kor + eng + math;
        double avg = total / 3.0;

        DataOutputStream out = new DataOutputStream(new FileOutputStream("/tmp/score.dat"));
        out.writeUTF(name);
        out.writeInt(kor);
        out.writeInt(eng);
        out.writeInt(math);
        out.writeDouble(total);
        out.writeDouble(avg);
        out.close();

    }
ublic class IOExam11 {
    public static void main(String[] args) throws Exception{
        // 문제 이름, 국어, 영어, 수학, 총첨 평균점수를 /tmp/score.dat 파일에서 읽으시오
        DataInputStream in = new DataInputStream(new FileInputStream("/tmp/score.dat"));
        String name = in.readUTF();
        int kor = in.readInt();
        int eng = in.readInt();
        int math = in.readInt();
        double total = in.readDouble();
        double avg = in.readDouble();
        in.close();

        System.out.println(name);
        System.out.println(kor);
        System.out.println(eng);
        System.out.println(math);
        System.out.println(total);
        System.out.println(avg);


    }
}

ByteArrayInputStream, ByteArrayOutputStream

byte[]에 데이터 읽고 쓰기

public class IOExam12 {
    public static void main(String[] args) throws Exception{
        int data1 = 1;
        int data2 = 2;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        out.write(data1); // data1의 마지막 1byte만 저장
        out.write(data2);
        out.close();

        byte[] array = out.toByteArray();
        System.out.println(array.length);
        System.out.println(array[0]);
        System.out.println(array[1]);
    }
}
public class IOExam13 {
    public static void main(String[] args) throws Exception{
        byte[] array = new byte[2];
        array[0] = (byte)1;
        array[1] = (byte)3;
        ByteArrayInputStream in = new ByteArrayInputStream(array);
        int read1 = in.read();
        int read2 = in.read();
        int read3 = in.read();
        in.close();

        System.out.println(read1);
        System.out.println(read2);
        System.out.println(read3);
    }
}

CharArrayReader, CharArrayWriter

char[]에 데이터를 읽고 쓰기

 

 

StringReader, StringWriter

문자열을 읽고 쓰기

public class IOExam14 {
    public static void main(String[] args) throws Exception{
        StringWriter out = new StringWriter();
        out.write("hello");
        out.write("world");
        out.write("!!!!");
        out.close();

        String str = out.toString();
        System.out.println(str);

    }
}
public class IOExam15 {
    public static void main(String[] args) throws Exception{
        StringReader in = new StringReader("helloworld!!");
        int ch = -1;

        while ((ch = in.read()) != -1){
            System.out.println((char)ch);
        }
        in.close();
    }
}