Day 20(8/29) Read(입력 받기)

2016. 8. 29. 15:38Programming/Java

**오늘 배운 내용**

1) System.in.read() 읽어오기

2) Line 단위로 read()하기

3) bean 클래스 이용법

4) 오브젝트 저장, 로드 방법



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.IOException;
 
public class L01Read {
     public static void main(String[] args){
  //System.in: 콘솔 창에서 입력을 받는 것
  //System.in.read()//콘솔창에서 입력받은 걸 읽어오는 것
  //read() => 하나씩만 읽을 수 있다
  //-> 입력 or 출력 (Input,Output IO)가 발생할 시 데이터를 쪼개고
  //변형하기 때문이다. 예) 문자열 -> 문자
 
      int input;
      try{
           while((input = System.in.read()) !=-1){//남아 있는 게 없을 때까지
           System.out.print((char)input); 
      }
      }catch(IOException e){}
   }
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
 
public class L02ReadLine {
     public static void main(String[] args) {
  //습관적으로 try catch 문에서 변수를 생성하지 마라.
  //Stream => 직렬화 버퍼에 임시 저장하기 위해 정렬하는 기술
  //Buffer => 임시 저장
      InputStreamReader isr = null;
      BufferedReader br = null;
      try{
           isr = new InputStreamReader(System.in);//실제로 입력이 이루어지진 않는다(보조 역할)
           br = new BufferedReader(isr);
      String line = "";
      int i=0;
      while((line = br.readLine())!=null){ //입력이 발생
          i++;
          System.out.println(i+":"+line);
      };
      }catch(IOException e){}
    }
}
cs




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
import java.io.Serializable;
 
public class UserInfo implements Serializable{
 //객체의 필드에 정보를 저장하기 위해 만들어진 클래스를 bean 클래스라 부른다.
 //beans, bean => 완두콩의 콩을 의미한다. => 저장과 관련된 것은 완두콩에 빚대어 표현
 //개발자들은 필드에 직접 접근할 수 없도록 private로 막는다.
 
     private String id;
     private String pw;
     private String name;
     private String email;
     private int level;
     private int age;
 
 public UserInfo(String id,String pw,String name,String enamal,int level,int age){
      this.id=id;
      this.pw=pw;
      this.name=name;
      this.email=email;
      this.level=level;
      this.age=age;
      }
      public void setPw(String pw){
          this.pw=pw;
      }
      public String getPw(){
         return pw;
      }
      public void levelUp(){
         ++level;
      }
      public String getId() { //객체를 저장하기 위한 beans 클래스;
         return id;
      }
     public void setId(String id) { //source => Generate getter&setter 통해서 쉽게 생성 가능
         this.id = id;
      }
}//class end
cs



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
import java.io.*//* 모든 클래스를 가져옴
 
class SaveObject{
     String fileName;
     FileOutputStream fos;
     BufferedOutputStream bos;
     ObjectOutputStream oos;
     public SaveObject(String fileName){
          this.fileName = fileName;
     }
     public void saveUser(UserInfo user) throws IOException{
          fos = new FileOutputStream(fileName);
          bos = new BufferedOutputStream(fos);
          oos = new ObjectOutputStream(bos);
          oos.writeObject(user);
          oos.close();
          System.out.println("저장 성공");
     }
}
 
public class L03SerialOut {
     public static void main(String[] args) {
      String folder = "src/com/javalesson/ch17Input_output/";
      String file = "user.ser";
  //저장
      SaveObject so = new SaveObject(folder+file);
      UserInfo user = new UserInfo("walker1991","kaya02","kimgo","naver.com",1,27);
      so.saveUser(user);
   }
}
cs



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
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.ObjectInputStream;
import java.io.IOException;
import java.lang.ClassNotFoundException;
 
class LoadObject{
     String fileName;
     FileInputStream fis=null;
     BufferedInputStream bis=null;
     ObjectInputStream ois=null;
 
     public LoadObject(String fileName){
          this.fileName=fileName;
     }
 
     public UserInfo loadUser() throws IOException, ClassNotFoundException{//void 상태 변경
          fis = new FileInputStream(fileName);
          bis = new BufferedInputStream(fis);
          ois = new ObjectInputStream(bis);//차례차례 거친다
          return (UserInfo)ois.readObject();
     }
}
 
public class L04SerialInput {
      public static void main(String[] args) {
      String fileName = "src/com/javalesson/ch17Input_output/user2.ser";
      LoadObject lo = new LoadObject(fileName);
    
      try{
      UserInfo user = lo.loadUser();
          System.out.println("유저 이름은: "+user.getName());
          System.out.println("유저 나이는: "+user.getAge());
          System.out.println("유저 아이디는: "+user.getId());
          System.out.println("유저 레벨은: "+user.getLevel());
          System.out.println("유저 이메일은: "+user.getEmail());
          System.out.println("유저 패스워드는: "+user.getPw());
      }catch(IOException e){ e.printStackTrace();
      }catch(ClassNotFoundException e){e.printStackTrace();}
   }
}
cs