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 |
'SOLUTION' 카테고리의 다른 글
17일차 과제풀이(ObjectOutputStream/ObjectInputStream 회원 아이디 찾기 기능_암호화한 정보(*)로 출력) (0) | 2020.02.09 |
---|---|
15일차 자바 IO클래스 관련 알고리즘 풀이 (0) | 2020.02.06 |
11일차 과제풀이 (0) | 2020.01.30 |
9일차 과제 풀이(정규사원과 임시사원의 정보를 상속의 클래스를 활용하여 출력) (0) | 2020.01.27 |
6일차 과제 풀이 (0) | 2020.01.20 |