Stream์ ์ด์ฉํ ์ค์ต
msg1์ ๋ด์ฉ์ msg2๋ก ๋ณด๋ด๊ณ msg2์ ๋ด์ฉ์ msg1์ผ๋ก ๋ณด๋ด๋ ๊ธฐ๋ฅ์ ๋ง๋ค์ด ๋ณด๋ผ
ํ๋ก๊ทธ๋จ ๊ตฌ๋ ์ ๋ฌธ์
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
|
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
public class StreamMain
{
public static void main(String[] args)
{
fileSwap();
}
private static void fileSwap() {
String fpath1 = "D:/java_test/msg1.txt";
String fpath2 = "D:/java_test/msg2.txt";
//msg1.txt์ ๋ด์ฉ์ ๋ก๋ํ์ฌ ๋ฎ์ด์ฐ๊ธฐ ๋์ง ์๋๋ก ํ๋ค.
try {
BufferedReader br1 = new BufferedReader(new FileReader(fpath1));
String msg1 = br1.readLine();
br1.close();
BufferedReader br2 = new BufferedReader(new FileReader(fpath2));
String msg2 = br2.readLine();
br2.close();
FileWriter fw1 = new FileWriter(fpath1);
fw1.write(msg2);
fw1.close();
FileWriter fw2 = new FileWriter(fpath2);
fw2.write(msg1);
fw2.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
cs |
์ฝ์ ์ ๋ณด๋ค์ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ ค ๋๊ณ ๋ฎ์ฌ์ฐ์ด์ง ์๊ฒ ํ๋ค. ๋ฐ๋ก ํ๋ฆฐํธ ํ๋ฉด ๋ด์ฉ์ด ์ง์์ง๊ธฐ์ ๊ทธ๋ผ ์ ๋ณด ๊ตํ์ด ๋์ง ์๋๋ค.
ํ๋ก๊ทธ๋จ ๊ตฌ๋ํ ๋ฐ๋ msg ๋ด์ฉ
Binary ๋ฐ์ดํฐ ๋ค๋ฃจ๊ธฐ
Bytes Stream: ๋ฌธ์ ์ด์ธ์ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃฐ ์ ์๋ ์คํธ๋ฆผ
์ง์ ํด์ค ์ฌ์ง ํ์ผ์ ๋ฐ์ดํธ ์๋ฅผ ๋ช๋ฒ ์ฝ๋์ง ํ์ธํด๋ณด๊ธฐ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import java.io.FileInputStream;
public class StreamMain
{
public static void main(String[] args)
{
String imgPath = "D:/java_test/health.jfif";
try {
FileInputStream fin = new FileInputStream(imgPath);
byte[] buf = new byte[1024];
int read = fin.read(buf);
while((read=fin.read(buf))!=-1) {
System.out.println(read);
}
//์ด๋ฏธ์ง์ ํ๋ฐ์ดํธ๋ 0~255๋ก ์ด๋ฃจ์ด์ ธ์๋ค.(2^8)
// 1๊ฐ ํ์์ 3๋ฐ์ดํธ (R 1๋ฐ์ดํธ G 1๋ฐ์ดํธ B 1๋ฐ์ดํธ)
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
cs |
์ฝ์๊ฐ:
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
1024
914
Byte ์คํธ๋ฆผ์ ์ฌ์ฉํ์ฌ ํ์ผ์ ์๋ ์ด๋ฏธ์ง ์๋ณธ์ ๋ณต์ฌํ์ฌ ํ์ผ์ ์ฌ๋ณธ ์ด๋ฏธ์ง ํ์ผ ์์ฑํ๊ธฐ
ํ์ผ ์ ๋ ฅ ์คํธ๋ฆผ๊ณผ ํ์ผ ์ถ๋ ฅ ์คํธ๋ฆผ์ ์ด์ฉํ์ฌ ์ฌ๋ณธ ์ด๋ฏธ์ง ๋ง๋ค๊ธฐ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import java.io.FileOutputStream;
import java.io.FileInputStream;
public class StreamMain
{
public static void main(String[] args)
{
String imgPath = "D:/java_test/health.jfif";
try {
FileInputStream fin = new FileInputStream(imgPath);
byte[] buf = new byte[1024];
FileOutputStream fout = new FileOutputStream("D:/java_test/health_2.jfif");
int read = 0;
while((read=fin.read(buf))!=-1) {
fout.write(buf, 0, read);
}
fin.close();
fout.close();
System.out.println("์ด๋ฏธ์ง ๋ณต์ฌ ์ฑ๊ณต");
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
cs |
ํ๋ก๊ทธ๋จ ์คํ ํ
ํ ์คํธ ๋ฐ์ดํฐ๋ฅผ Byte ์คํธ๋ฆผ์ผ๋ก ์ฝ์ด์ ํ๋ฉด์ ํ ์คํธ๋ก ์ถ๋ ฅํ๊ธฐ
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import java.io.File;
import java.io.FileInputStream;
public class StreamMain
{
public static void main(String[] args)
{
File f = new File("D:/java_test/info.txt");
int len = (int)f.length(); //ํ์ผ์ ํฌ๊ธฐ๋ฅผ ์ ์ ์๋ค.
System.out.println("ํ์ผ ํฌ๊ธฐ" + len);
try {
FileInputStream fin = new FileInputStream(f);
byte[] buf = new byte[len];
int read = fin.read(buf);
System.out.println("์ฝ์ด์จ ๋ฐ์ดํธ ์" + read);
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
cs |
์ค์ ํ์ผ์ ํฌ๊ธฐ
์ฝ์ ๊ฐ:
๋ฌธ์์ด ์์ฑ์๋ฅผ ์ฐ๋ฉด Byte ์คํธ๋ฆผ์ผ๋ก ์ฝ์ด์จ ๋ฐ์ดํฐ๋ฅผ ๋ฌธ์๋ก ์ถ๋ ฅํด ๋ณผ ์ ์๋ค.
ํํฐ์คํธ๋ฆผ์ ์ถ๊ฐํ์ฌ Byte ์คํธ๋ฆผ์ ์ด์ฉํ์ฌ ๋ฌธ์์ด ์์ฑ์๋ฅผ ์์ฐ๊ณ ๋ฌธ์ ์ถ๋ ฅํด๋ณด๊ธฐ
์๋ณธ ๋ฌธ์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class StreamMain
{
public static void main(String[] args)
{
File f = new File("D:/java_test/info.txt");
try {
FileInputStream fin = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fin);
int ch = isr.read();
System.out.println((char)ch);
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
cs |
InputStreamReader์ ํํฐ์คํธ๋ฆผ(๊ฐ๊ณต์คํธ๋ฆผ)์ผ๋ก ๋ฌธ์๋ฅผ ๋ฐ์ดํธ ์คํธ๋ฆผ์ผ๋ก ์ฝ์์ ๋ ๋ค์ ๋ฌธ์๋ก ๋ฐ๊ฟ์ค๋ค. ์ด ๋ ํ๊ธ์๋ง ์ฝ์ด ์์ผ๋ฏ๋ก ์ฝ์๊ฐ์๋ ์๋ณธ ๋ฌธ์์ ์ ์ผ ์ฒซ๋ฒ์งธ ๊ธ์์ธ 1์ด ๋์๋ค.
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.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class StreamMain
{
public static void main(String[] args)
{
File f = new File("D:/java_test/info.txt");
try {
FileInputStream fin = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fin);
BufferedReader br = new BufferedReader(isr);
String line = null;
while((line=br.readLine())!=null) {
System.out.println(line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
cs |
์ฝ์ ๊ฐ:
1|ํด๋ฆฐ|01066665555
2|๋ฏผ์ง|01066667777
3|ํ๋|01033335555
4|ํ๋ฆฐ|01022225555
5|๋ค๋์|01055558888
๋ฐ์ดํธ๋ฐฐ์ด๋ก ์ด๋ฏธ์ง๋ฅผ ๋ก๋ํด์ ํ๋๋์คํฌ์ ์ ์ฅํด๋ณด๊ธฐ
ByteArrayInputStream / OutputStream
์ด๋ฏธ์ง ํ์ผ์ ๋ฉ๋ชจ๋ฆฌ์ byte[]ํ์์ผ๋ก ๋ก๋
FileInputStream → ByteArrayOutputStream(๋ฉ๋ชจ๋ฆฌ์ byte[]์ ์ถ๋ ฅ)
write() ๊ธฐ๋ฅ์ ์คํํ ๋ read ํฌ๊ธฐ๋งํผ ์ฐ๊ฒ๋ ํด์ ์ฐ๋ ๊ธฐ ๋ฐ์ดํฐ๊ฐ ๋ ๋ถ์ง์๊ณ ์๋ณธ๊ณผ ๋๊ฐ์ ํฌ๊ธฐ์ ํ์ผ์ ๋ณต์ฌํ ์ ์์๋ค.
writeImage ๋ฉ์๋์ ์์ img ๋ฐฐ์ด์ ์ ๋ฌํ์ฌ ๊ทธ ๋ฉ์๋์์์ ํ์ผ์ ์ฐ๊ฒํ๋ค.
ํ์ผ๋ช ์ imgCpy
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.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Stream2Main
{
public static void main(String[] args)
{
File imgFile = new File("D:/java_test/health.jfif");
System.out.println("ํ์ผ ์กด์ฌ?:"+ imgFile.exists());
//ํ์ผ์ด ์กด์ฌํ๋์ง ์ ๋ฌด ํ์ธ
try {
FileInputStream fin = new FileInputStream(imgFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
while(true) {
int read = fin.read(buf);
if(read==-1) break;
bos.write(buf, 0, read);
}
byte[] img = bos.toByteArray(); //์ด๋ฏธ์ง ์ ๋ณด๊ฐ ๋ชจ๋ ๋ด๊ธด ๋ฐ์ดํธ ๋ฐฐ์ด
System.out.println("ํ์ผ ์ฌ์ด์ฆ?:"+ img.length);
writeImage(img);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void writeImage(byte[] img) {
try {
FileOutputStream fout = new FileOutputStream("D:/java_test/imgCpy.jfif");
fout.write(img, 0, img.length);
fout.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
cs |
๊ฒฐ๊ณผ ํ์ธ:
์์ฑ๋ ์ด๋ฏธ์ง๋ฅผ ํ์ธํ ์ ์๋ค. ๊ธฐ๋ฅ๋ณ๋ก ๋ฉ์๋๋ฅผ ๋๋ ์ ์๋ค.
โ์ง๋ ฌํโ
๊ฐ์ฒด์ ์ง๋ ฌํ (Object Serialization) : ์ค๋ธ์ ํธ๋ฅผ ๋ค๋ฅธ ์ปดํจํฐ๋ก ์ ์กํ ๋, ๋์คํฌ์ ๊ฐ์ฒด๋ฅผ ์ ์ฅํ ๋ ์ฐ์ธ๋ค.
์ญ์ง๋ ฌํ De - Serialization :์ง๋ ฌํ ๋ ๊ฐ์ฒด๋ฅผ ๋ค์ ๋ณต์ํ ๋ ์ฐ์ธ๋ค.
์์
๊ฐ์ฒด๋ฅผ ์ง๋ ฌํํ์ฌ ํ์ผ์ ์ ์ฅํ๊ธฐ emp.obj ํ์ผ์ด ์์ฑ๋ ๊ฑธ ํ์ธํ ์ ์๋ค.
์ด๋ ์ง๋ ฌํํ ๊ฐ์ฒด๋ Serializable ๋งํฌ ์ธํฐ ํ์ด์ค๋ฅผ ๋ฌ์์ค์ผํ๋ค.
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
|
import java.io.Serializable;
public class Employee implements Serializable //๋งํฌ ์ธํฐํ์ด์ค
{
private int empno;
private String ename;
private int deptno;
private String hiredate;
public Employee(String[] strings) {}
public Employee() {}
public Employee(int empno, String ename, int deptno, String hiredate) {
this.setEmpno(empno);
this.setEname(ename);
this.setDeptno(deptno);
this.setHiredate(hiredate);
}
public String Employeelist() {
return String.format("%d\t%s\t%d\t%s",
empno,ename,deptno,hiredate);
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getHiredate() {
return hiredate;
}
public void setHiredate(String hiredate) {
this.hiredate = hiredate;
}
}
|
cs |
๋ค์ ์ญ์ง๋ ฌํ ํ์ฌ ๋์คํฌ์ ์ ์ฅ๋ emp.obj ํ์ผ์ ๊ฐ์ฒด๋ก์ ํ์ธํ๊ธฐ
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
|
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import com.ezen.javaoop.Employee;
public class SerializationTest
{
public static void main(String[] args)
{
Employee emp = new Employee();
//์ง๋ ฌํ ํ ๊ฐ์ฒด๋ ๊ฐ๋ฐ์๊ฐ ํด๋์ค์ ํ์ํด์ค์ผ ํ๋ค.(๋งํฌ ์ธํฐํ์ด์ค)
emp.setEmpno(11);
emp.setEname("์ฃผํ");
emp.setDeptno(20);
emp.setHiredate("22-10-27");
System.out.println("์๋ณธ ์ค๋ธ์ ํธ:"+emp.Employeelist());
try {
FileOutputStream fout = new FileOutputStream("D:/java_test/emp.obj");
ObjectOutputStream obj = new ObjectOutputStream(fout);
obj.writeObject(emp); //๊ฐ์ฒด ์ง๋ ฌํ ๋ฐ ํ์ผ์ ์ ์ฅ
System.out.println("Employee ์ค๋ธ์ ํธ ์ง๋ ฌํ ์ฑ๊ณต");
} catch (Exception e) {
System.err.println("Employee ์ค๋ธ์ ํธ ์ง๋ ฌํ ์คํจ");
e.printStackTrace();
}
deSerialization("D:/java_test/emp.obj");
}
public static void deSerialization(String objPath) {
try {
FileInputStream fin = new FileInputStream(objPath);
ObjectInputStream oin = new ObjectInputStream(fin);
Employee emp = (Employee)oin.readObject();
System.out.println("๋ณต์๋ Employee ๊ฐ์ฒด:"+ emp.Employeelist());
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
cs |
์ฝ์ ๊ฐ:
์๋ณธ ์ค๋ธ์ ํธ:11 ์ฃผํ 20 22-10-27
Employee ์ค๋ธ์ ํธ ์ง๋ ฌํ ์ฑ๊ณต
๋ณต์๋ Employee ๊ฐ์ฒด:11 ์ฃผํ 20 22-10-27
๋ฆฌ์คํธ์ Employee ์ค๋ธ์ ํธ๋ฅผ ํ๋ ์ถ๊ฐํ๊ณ ๋ฆฌ์คํธ๋ฅผ ์ง๋ ฌํ ํ์ฌ ํ์ผ์ ์ ์ฅํํ ๋ค์ ๋ฆฌ์คํธ๋ฅผ ๋ณต์ํ์ฌ ๋ฆฌ์คํธ์์ Employee ์ค๋ธ์ ํธ๊ฐ ์ ๋๋ก ์๋ํ๊ฒ ํด๋ณด์์ค.
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
|
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import com.ezen.javaoop.Employee;
public class SerializationTest
{
public static void main(String[] args)
{
listSerialize();
listdeSerialization("D:/java_test/emplist.obj");
}
public static void listSerialize() {
ArrayList<Employee> list = new ArrayList<>();
list.add(new Employee(2,"๊ฐํด๋ฆฐ",30,"22-10-27"));
try {
File f = new File("D:/java_test/emplist.obj");
FileOutputStream fout = new FileOutputStream(f);
ObjectOutputStream obj = new ObjectOutputStream(fout);
obj.writeObject(list);
System.out.println("Employee list ์ค๋ธ์ ํธ ์ง๋ ฌํ ์ฑ๊ณต");
} catch (Exception e) {
System.err.println("Employee list ์ค๋ธ์ ํธ ์ง๋ ฌํ ์คํจ");
e.printStackTrace();
}
}
public static void listdeSerialization(String f) {
try {
FileInputStream fin = new FileInputStream(f);
ObjectInputStream oin = new ObjectInputStream(fin);
ArrayList<Employee> list2 = (ArrayList<Employee>)oin.readObject();
System.out.println("๋ณต์๋ Employee list ๊ฐ์ฒด:"+ list2.get(0));
} catch (Exception e) {
e.printStackTrace();
}
}
}
|
cs |
์์ฑ๋ ํ์ผ:
์ฝ์ ๊ฐ:
Employee list ์ค๋ธ์ ํธ ์ง๋ ฌํ ์ฑ๊ณต
๋ณต์๋ Employee list ๊ฐ์ฒด:2 ๊ฐํด๋ฆฐ 30 22-10-27
๋๊ธ