๐Ÿ’ป

Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ - byte ์ž…์ถœ๋ ฅ, char์ž…์ถœ๋ ฅ ๋ณธ๋ฌธ

KITRI/JAVA

Java ํ”„๋กœ๊ทธ๋ž˜๋ฐ - byte ์ž…์ถœ๋ ฅ, char์ž…์ถœ๋ ฅ

๋˜ํšจ๋‹ˆ 2020. 5. 20. 13:03

์ž…๋ ฅ ์ŠคํŠธ๋ฆผ : ํ”„๋กœ๊ทธ๋žจ์ด ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์„ ๋•Œ

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();
			}
		}
	}
}
๋ฐ˜์‘ํ˜•
Comments