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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package day17_io;
 
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
import java.io.IOException;
 
/*
 * 1. 학생번호,이름,전화번호를 키보드로 입력받아 student.data파일로 저장하세요.
   student.data파일의 학생정보를 모두 출력해 보세요.
   ==> DataOutputStream,DataInputStream사용
 */
 
public class Task1 {
    public static void main(String[] args) {
        try {
            int count = output();
            input(count);
        } catch (IOException ie) {
            System.out.println(ie.getMessage());
        }
    }
 
    public static int output() throws IOException {
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("student.data"));
        System.out.println("전체학생정보를 출력하기 위해 학생정보를 입력하세요.");
        Scanner scan = new Scanner(System.in);
        System.out.println("입력할 학생 수를 입력하세요.");
        int count = scan.nextInt();
        for(int i=1; i<=count; i++) {
            System.out.print(i+"번째 학생번호를 입력하세요:");
            dos.writeUTF(scan.next());
            System.out.print(i+"번째 학생이름을 입력하세요:");
            dos.writeUTF(scan.next());
            System.out.print(i+"번째 학생 전화번호를 입력하세요:");
            dos.writeUTF(scan.next());
        }
        dos.close();
        scan.close();
        return count;
    }
    public static void input(int count) throws IOException{
        DataInputStream dis = new DataInputStream(new FileInputStream("student.data"));
        System.out.println("<< 전체학생정보 >>");
            for(int i=1; i<=count; i++) {
                String readNum = dis.readUTF();
                String readName = dis.readUTF();
                String readTel = dis.readUTF();
                System.out.println(i+"번째 학생번호:"+readNum);
                System.out.println(i+"번째 학생이름:"+readName);
                System.out.println(i+"번째 전화번호:"+readTel);
                System.out.println("========================");
        }
            dis.close();
    }
}
 
cs

+ Recent posts