티스토리 뷰

1. 직렬화와 역직렬화란? 

직렬화 Serialization
object를 binary 형태의 파일로 바꾸는거
컴퓨터는 0과 1로 구성되어 있기 때문에 binary형태로 바꿔주는것이 필요
.bin이나 ser(Serialization)


역직렬화 Deserialization
Binary형태의 파일을 Object로 생성하는것


직렬화가 필요한이유?
- 저장이 필요하기 때문에

 

 

* JAVA

 

http://javadevwannabe.blogspot.com/2012/02/high-level-streams.html

 

- InputStream

1
2
3
4
FileOutputStream fsOut = new FileOutputStream("binFile.bin");
ObjectOutputStream osOut = new ObjectOutputStream(fsOut);
osOut.writeObject(student);
osOut.close(); //close를 하지 않으면 메모리 누수 현상 발생 
cs

 

- OutputStream

1
2
3
4
 
FileInputStream fsIn = new FileInputStream("binFile.bin");
ObjectInputStream osIn = new ObjectInputStream(fsIn);
Student clone = (Student) osIn.readObject();     
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package programming;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
 
class Student implements Serializable{
    /**
     * 
     */
    
    private static final long serialVersionUID = 1112261190310944806L;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMajor() {
        return major;
    }
    public void setMajor(String major) {
        this.major = major;
    }
    
    public Student(int id, String name, String major) {
        this.id=id;
        this.name = name;
        this.major = major; 
    }
        
    private int id;
    private String name;
    private String major;
        
}
 
 
 
public class programm {
 
    public static void main(String[] args) throws IOException, ClassNotFoundException {
 
        
        Student student = new Student(7777,"name","major");
        System.out.println(student.getId());
        
        
        FileOutputStream fsOut = new FileOutputStream("binFile.bin");
        ObjectOutputStream osOut = new ObjectOutputStream(fsOut);
        osOut.writeObject(student);
        osOut.close(); //close를 하지 않으면 메모리 누수 현상 발생 
 
 
        FileInputStream fsIn = new FileInputStream("binFile.bin");
        ObjectInputStream osIn = new ObjectInputStream(fsIn);
        Student clone = (Student) osIn.readObject();         
        
        osIn.close();
        
        System.out.println(clone.getId() + " " + clone.getMajor());
    
    }
    
 
    
}
cs

 

 

* C#

* python 

 

 

 

2. 텍스트 파일 읽고 쓰기

*JAVA

 

# Write

- BufferedWriter 사용 

- write("~~");  // 쓰기

- newLine(); // Enter 

- flush(); // 기억하고 있다가 메모장에 사용 

- close(); // JAVA의 경우 열면 다시 닫아줘야함. 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
//Write
Path note = Paths.get("note.txt");
try {
    BufferedWriter writer = Files.newBufferedWriter(note, StandardCharsets.UTF_8 );
    writer.write("Today's weather is good"); 
    writer.newLine();
    writer.write("How are you? ");
    writer.flush(); //memory and write 
    writer.close();             
catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
cs

 

# Read 

- BufferedReader 사용 

- readLine(); 

- close(); Buffered사용시 닫기 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Path book = Paths.get("book.txt");
 
try {
    
    BufferedReader reader = Files.newBufferedReader(book);
    System.out.println(reader.readLine());
    System.out.println(reader.readLine());
    
    //반복문을 통해서 읽어본다
    String line = reader.readLine();
            
    while(line!=null) {
        System.out.println(line);
        line = reader.readLine(); 
        
    }
 
    reader.close();
 
catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    System.out.println("could not read the file");
}
cs

 

 

 

 

 

 

<출처>

1.www.youtube.com/watch?v=BlMttZXA2cc&list=PLENYGEQnz1xrMzGAfcCJFBzkBNzY2ufb1&index=37

2. 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG more
«   2024/11   »
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
글 보관함