버글버글
Java 수업 기록 (24) IO - OutputStream 본문
반응형
▶ IO
▶ OutputStream
1. 바이트 출력 스트림
2. 1개 : int
3. 여러 개 : byte
예시)
File file = new File("c:\\storage", "b1.bin");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
int c = 'A';
String str = "pple Mango 맛있다.";
byte[] b = str.getBytes(StandardCharsets.UTF_8);
fos.write(c);
fos.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fos != null) fos.close();
} catch (IOException e) {
e.printStackTrace();
}
▶ OutputStream - BufferedOutputStream
File file = new File("c:\\storage", "b2.bin");
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
String str = "안녕하세요 반갑습니다.";
byte[] b = str.getBytes("UTF-8");
bos.write(b);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(bos != null) bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
▶ DataOutputStream
1. 변수를 그대로 출력
2. 변수 선언후 출력할때는
데이터타입 | 출력 |
String name = "에밀리"; | dos.writeUTF(name); |
int age = 30; | dos.writeInt(age); |
double height = 165.5; | dos.writeDouble(height); |
예시)
File file = new File("C:\\storage", "b3.dat");
FileOutputStream fos = null;
DataOutputStream dos = null;
try {
fos = new FileOutputStream(file);
dos = new DataOutputStream(fos);
String name = "에밀리";
int age = 30;
double height = 165.5;
dos.writeUTF(name);
dos.writeInt(age);
dos.writeDouble(height);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dos != null) dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
▶ ObjectOutputStream
1. 객체를 그대로 출력하는 보조 스트림(?)
예시) User 클래스
import java.io.Serializable;
// 스트림을 이용해서 객체를 전송하려면 직렬화를 해야 한다.
// 직렬화가 필요한 객체는 Serializable 인터페이스를 구현해야 한다.
// Serializable 인터페이스를 구현한 클래스는 SerialBersionUID 필드가 필요하다.
public class User implements Serializable {
private static final long serialVersionUID = -1830845902387248224L;
private int userNo;
private String name;
private int age;
public User(int userNo, String name, int age) {
super();
this.userNo = userNo;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User [userNo=" + userNo + ", name=" + name + ", age=" + age + "]";
}
try {
// 1. User 객체를 3개 저장한 ArrayList
List<User> users = Arrays.asList(
new User(1, "kim" ,30),
new User(2, "lee", 40),
new User(3, "choi", 50)
);
// 2. User 1개
User user =new User(4, "min", 60);
fos = new FileOutputStream(file);
oos = new ObjectOutputStream(fos);
oos.writeObject(users);
oos.writeObject(user);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null) oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
▶ 기타사항
1. 직렬화가 가능하려면 Serializable interface를 구현해줘야 한다.
2. finally 는 (write, outstream) 은 꼭..
반응형
'java > java 수업 기록' 카테고리의 다른 글
Java 수업 기록 (26) Network (0) | 2022.08.18 |
---|---|
Java 수업 기록 (25) IO - InputStream (0) | 2022.08.17 |
Java 수업 기록 (23) IO - Reader (0) | 2022.08.15 |
Java 수업 기록 (22) IO - Writer (0) | 2022.08.14 |
Java 수업 기록 (21) IO - File (0) | 2022.08.13 |