⇒ 다른 데이터를 포함하는 데이터를 표현하기 힘들다. 즉, CSV 형식은, 계층 구조로 된 데이터를 표현하기 어렵다.
<book>
<title> --- </title>
<description>---</description>
<authors>
<author>
<name>---</name>
<email>---</email>
<tel>---</tel>
</author>
<author>
<name>---</name>
<email>---</email>
<tel>---</tel>
</author>
</authors>
<press>
<name>---</name>
<homepage>---</homepage>
<tel>---</tel>
</press>
</book>
<book>---</book>
: 데이터를 설명하는 데이터<book>
: 시작 태그---
: content</book>
: 끝 태그<book>
과 <authors>
는 <name>
입장에서 조상(ancestor) 태그이다.<author>
는 <name>
입장에서 부모 태그이다.<name>
은 <author>
입장에서 자식태그이고, <book>
과 <authors>
입장에서는 자손(descendant) 태그이다.JSON(Java Script Object Notation): 자바스크립트에서 객체를 표기하는 문법을 차용해서 데이터를 표현하는 문법으로 만든 것.
{
"title": "----",
"description": "----",
"authors": [{"name": "----","email": "----", "tel": "----"},
{"name": "----","email": "----", "tel": "----"}],
"press": {"name": "----",
"homepage": "----",
"tel": "----"}
}
dependencies {
implementation 'com.google.code.gson:gson:2.9.1'
testImplementation 'junit:junit:4.13.2'
implementation 'com.google.guava:guava:30.1.1-jre'
}
gradle eclipse
명령을 // 게시글 목록을 관리하는 역할
//
public class BoardDao {
...
public void load() throws Exception {
try (BufferedReader in = new BufferedReader(new FileReader(filename))) {
// 파일에서 JSON 문자열을 모두 읽어 StringBuilder에 담는다.
StringBuilder stringBuilder = new StringBuilder();
String str;
while ((str = in.readLine()) != null) {
stringBuilder.append(str);
}
// StringBuilde에 보관된 JSON 문자열을 가지고 Board[]을 생성한다./
Board[] arr = new Gson().fromJson(stringBuilder.toString(), Board[].class);
// Board[]배열의 저장된 객체를 List로 옮긴다.
for(int i=0;i<arr.length;i++) {
list.add(arr[i]);
}
}
}
public void save() throws Exception {
try (FileWriter out = new FileWriter(filename)) {
Board[] boards = list.toArray(new Board[0]); // 현재 list에 들어 있는 보드 객체들 받아오기
//String json = new Gson().toJson(boards); // 이렇게 코딩 안하고, 그냥 바로 넣어버린다.
out.write( new Gson().toJson(boards));
}
}
...
}
// 회원 목록을 관리하는 역할
//
public class MemberDao {
...
public void load() throws Exception {
try(BufferedReader in = new BufferedReader(new FileReader(fileName))){
StringBuilder stringBuilder = new StringBuilder();
String str;
while ((str = in.readLine()) != null) {
stringBuilder.append(str);
}
Member[] members = new Gson().fromJson(stringBuilder.toString(),Member[].class);
for(int i=0;i<members.length;i++) {
list.add(members[i]);
}
}
}
public void save() throws Exception{
try(FileWriter out = new FileWriter(fileName)){
Member[] members = list.toArray(new Member[0]);
out.write(new Gson().toJson(members));
}
}
...
}
C/S Application(Client/Server Application): Application을 이원화 한 것
- 다중 사용자에게 동시 서비스 제공 가능
- 다중 Client(사용자)의 요청을 한 곳에서 처리 ⇒ 데이터 공유 가능
java.net.Socket 이용 → 두 application이 데이터를 주고 받을 수 있도록 연결한다.
➕
데이터 입출력(I/O)를 위해서 java.io.* ← Stream 클래스의 도움 받기
gradle eclipse
명령을 수행하여 이클립스 설정 파일을 준비한다.eclipse {
project {
name = "board-app-client"
// 프로젝트 이름을 지정하지 않으면 build.gradle 파일이 있는
// 폴더 이름을 프로젝트 이름을 사용한다.
}
...
}
eclipse {
project {
name = "board-app-server"
// 프로젝트 이름을 지정하지 않으면 build.gradle 파일이 있는
// 폴더 이름을 프로젝트 이름을 사용한다.
}
...
}