๐ป
Java ํ๋ก๊ทธ๋๋ฐ - byte ์ ์ถ๋ ฅ, char์ ์ถ๋ ฅ ๋ณธ๋ฌธ
์ ๋ ฅ ์คํธ๋ฆผ : ํ๋ก๊ทธ๋จ์ด ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅ๋ฐ์ ๋
ex) ํค๋ณด๋, ํ์ผ, ํ๋ก๊ทธ๋จ
โ
์ถ๋ ฅ ์คํธ๋ฆผ : ํ๋ก๊ทธ๋จ์ด ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ผ ๋
ex) ๋ชจ๋ํฐ, ํ์ผ, ํ๋ก๊ทธ๋จ
โ
- ์คํธ๋ฆผ์ ํน์ฑ์ด ๋จ๋ฐฉํฅ์ด๋ฏ๋ก ํ๋์ ์คํธ๋ฆผ์ผ๋ก ์ ๋ ฅ๊ณผ ์ถ๋ ฅ์ ๋ชจ๋ ํ ์ ์๋ค.
โ
์คํธ๋ฆผ ํด๋์ค ์ข ๋ฅ
โ
1. ๋ฐ์ดํธ(byte) ๊ธฐ๋ฐ ์คํธ๋ฆผ
โ
- ๊ทธ๋ฆผ, ๋ฉํฐ๋ฏธ๋์ด, ๋ฌธ์ ๋ฑ ๋ชจ๋ ์ข ๋ฅ์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ๊ณ ๋ณด๋ผ ์ ์์
- ์ต์์ ํด๋์ค : InputStream(์ ๋ ฅ ์คํธ๋ฆผ) // OutputStream(์ถ๋ ฅ ์คํธ๋ฆผ)
โ
2. ๋ฌธ์(character)๊ธฐ๋ฐ ์คํธ๋ฆผ
โ
- ์ค๋ก์ง ๋ฌธ์๋ง ๋ฐ๊ณ ๋ณด๋ผ ์ ์์
- ์ต์์ ํด๋์ค : Reader(์ ๋ ฅ ์คํธ๋ฆผ) // Writer(์ถ๋ ฅ ์คํธ๋ฆผ)
โ
package io02.Byte;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @Author : ๊น์ํ
* @Date : 2020. 5. 18.
* @Description : ํ์ผ ์
์ถ๋ ฅ: ๊ทธ๋ฆผ, ๋ฉํฐ๋ฏธ๋์ด, ๋ฌธ์ ์ข
๋ฅ์ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ผ ์ ์๋ค.
* ๋ฐ์ดํธ๊ธฐ๋ฐ, ๋ฌธ์๊ธฐ๋ฐ ์คํธ๋ฆผ์ผ๋ก ๋๋๋ค.
*
* ๋ฐ์ดํธ๊ธฐ๋ฐ : ๊ทธ๋ฆผ, ๋ฉํฐ๋ฏธ๋์ด, ๋ฌธ์์ ๋ชจ๋ ์ข
๋ฅ ๋ฐ์ดํฐ๋ฅผ ๋ณด๋ด๊ณ ๋ฐ์ ์ ์๋ค.
*
*/
public class ByteEx01 {
public static void main(String[] args) {
byte[] data = new byte[] {'a','p','p','l','e'}; //ํ์ผ ๋๋ ํค๋ณด๋ ๋ฐ์ดํฐ
try {
File file = new File("C:\\sohyunkim\\java\\fileUpDown\\ex01.txt");
FileOutputStream fos = new FileOutputStream(file);
for(int i=0; i<data.length; i++) {
fos.write(data[i]);
}
fos.flush(); //๋ฒํผ์ ์๋ฅํ๊ณ ์๋ ๋ฐ์ดํธ๋ฅด ๊ฐ์ ๋ก ์ถ๋ ฅํ๋ค.
fos.close(); //์์คํ
์์ ๋ฐ๋ฉํ๊ณ ์ถ๋ ฅ์คํธ๋ฆผ ๋ซ๋๋ค.(close ์ํ ๊ฒฝ์ฐ ๋ฐ์ดํฐ ์์ค)
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
}
package io02.Byte;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @Author : ๊น์ํ
* @Date : 2020. 5. 19.
* @Description : BufferedInputStream - ๋ณด์กฐ์คํธ๋ฆผ: ์ผ์ ๋ฐ์ดํธ ๋ชจ์์ ์
์ถ๋ ฅ
*/
public class ByteEx06 {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
File infile;
File outFile;
try {
infile = new File("C:\\sohyunkim\\java\\fileUpDown\\input\\1234.xls");
fis = new FileInputStream(infile);
bis = new BufferedInputStream(fis, 1024); //์ผ๋ฐํ์ผ 1024, ๋คํธ์ํฌ๋ 512
outFile = new File("C:\\sohyunkim\\java\\fileUpDown\\output\\1234.xls");
fos = new FileOutputStream(outFile);
bos = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[65536];
while(true) {
int count = bis.read(data);
if(count == -1) break;
bos.write(count);
}
bos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(fos!=null) fos.close();
if(fis!=null) fis.close();
if(bis !=null) bis.close();
if(bos !=null) bos.close();
} catch(IOException e){
e.printStackTrace();
}
}
}
}
package io03.Char;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;
/**
* @Author : ๊น์ํ
* @Date : 2020. 5. 19.
* @Description : ๋ฌธ์ ์
์ถ๋ ฅ - Read & Writer
*/
public class CharEx01 {
public static void main(String[] args) {
FileWriter fw = null;
BufferedWriter bw = null;
Scanner sc = null;
File file = null;
try {
file = new File("C:\\sohyunkim\\java\\fileUpDown\\output\\charEx01.txt");
fw = new FileWriter(file);
bw = new BufferedWriter(fw, 1024);
System.out.println("๋ฌธ์์ด์ ์
๋ ฅํ์ธ์:");
sc = new Scanner(System.in);
while(true) {
String msg = sc.nextLine(); //ํ ์ค์ฉ ์ฝ์ด์จ๋ค.
if(msg.equalsIgnoreCase("q")) break;
bw.write(msg + "\r\n");
bw.flush();
}
System.out.println("๋ด์ฉ์ด ์ถ๋ ฅ๋์์ต๋๋ค.");
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if(fw !=null) fw.close();
if(bw !=null) bw.close();
if(sc !=null) sc.close();
} catch (Exception e){
e.printStackTrace();
}
}
}
}
package io03.Char;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class CharEx02 {
public static void main(String[] args) {
File file = null;
FileReader fr = null;
BufferedReader br = null;
try {
file = new File("C:\\sohyunkim\\java\\fileUpDown\\output\\charEx01.txt");
fr = new FileReader(file);
br = new BufferedReader(fr, 1024);
while(true) {
String str = br.readLine();
if(str==null) break;
System.out.println(str);
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
try {
if(fr!=null) fr.close();
if(br!=null) br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package io03.Char;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class CharEx03 {
public static void main(String[] args) {
File file = null;
FileWriter fw = null;
BufferedWriter bw = null;
PrintWriter pw = null; //์ถ๋ ฅ์ ๋๋ ํ๋ฆฐํธ ๋ณด์กฐ ์คํธ๋ฆผ PrintWriter(๋ฌธ์), PrintStream(๋ฐ์ดํธ)
Scanner sc = null;
FileReader fr = null;
BufferedReader br = null;
try {
file = new File("C:\\sohyunkim\\java\\fileUpDown\\output\\chEx03.txt");
fw = new FileWriter(file);
bw = new BufferedWriter(fw, 1024);
pw = new PrintWriter(bw);
//์ถ๋ ฅ
System.out.println("๋ฌธ์์ด์ ์
๋ ฅํ์ธ์.");
sc = new Scanner(System.in);
while(true) {
String msg = sc.nextLine();
if(msg.equalsIgnoreCase("q")) break;
pw.println(msg);
pw.flush();
}
System.out.println("\n ์ถ๋ ฅ์๋ฃ \n");
//์
๋ ฅ
fr = new FileReader(file);
br = new BufferedReader(fr, 1024);
while(true) {
String msg = br.readLine();
if(msg==null) break;
System.out.println(msg);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(fw!=null) fw.close();
if(bw!=null) bw.close();
if(sc!=null) sc.close();
if(fr!=null) fr.close();
if(br!=null) br.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
}
๋ฐ์ํ
'KITRI > JAVA' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Java ํ๋ก๊ทธ๋๋ฐ - ๋คํธ์ํฌ ํต์ (0) | 2020.05.25 |
---|---|
Java ํ๋ก๊ทธ๋๋ฐ - ์ค๋ ๋(Thread) (0) | 2020.05.20 |
Java ํ๋ก๊ทธ๋๋ฐ - ์์ธ์ฒ๋ฆฌ(Exception) (0) | 2020.05.18 |
Java ํ๋ก๊ทธ๋๋ฐ - MVC ํจํด (0) | 2020.05.15 |
Java ํ๋ก๊ทธ๋๋ฐ - ์ปฌ๋ ์ ํ๋ ์์ํฌ(List, Set, Map) (0) | 2020.05.14 |
Comments