JAVA

[JAVA] 입출력(IO)

기정님 2025. 4. 29. 14:25

▶ 입출력(IO)

Input과 Output의 약자, 컴퓨터 내부 또는 외부 장치와 프로그램 간의 데이터를 주고 받는 것 장치와 입출력을 위해서는 하드웨어 장치에 직접 접근이 필요한데 다양한 매체에 존재하는 데이터들을 사용하기 위해 입출력 데이터를 처리할 공통적인 방법으로 스트림 이용


▶ 스트림(Stream)

입출력 장치에서 데이터를 읽고 쓰기 위해서 자바에서 제공하는 클래스 모든 스트림은 단방향이며 각각의 장치마다 연결할 수 있는 스트림 존재 하나의 스트림으로 입출력을 동시에 수행할 수 없으므로 동시에 수행하려면 2개의 스트림 필요


▶ 스트림 종류 * 색이 있는 것은 기반 스트림, 색이 없는 것은 보조 스트림


▶ InputStream


▶ OutputStream


▶ Reader


▶ Writer


▶ File 클래스

	public void method() {

		try {
			File f1 = new File("test.txt");
			f1.createNewFile();
			
			File f2 = new File("C:\\test\\test.txt"); //존재하는 폴더에 파일 만들기
			f2.createNewFile();
			
			File f3 = new File("C:\\temp1\\temp2"); //폴더 지정
			f3.mkdirs(); //폴더만들기
			
			File f4 = new File(f3, "test.txt"); //f3폴더에 파일 넣기
			f4.createNewFile(); //파일 만들기
			f4.delete();
			
			System.out.println(f2.exists()); //존재하니?
			System.out.println(f3.exists()); //존재하니?
			System.out.println(f4.exists()); //존재하니?
			System.out.println(f3.isFile()); //파일형태니?
			
			System.out.println("파일 명 : " + f1.getName());
			System.out.println("절대경로 : " + f1.getAbsolutePath());
			System.out.println("상대경로 : " + f1.getPath());
			System.out.println("파일용량 : " + f1.length());
			System.out.println("상위폴더 : " + f4.getParent());
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}

new file / createNewFile가 말 그대로 파일만 만들어주지 경로에서 폴더까지는 만들어주지않는다

 


▶ FileInputStream

▶ FileOutputStream

▶ FileReader

▶ FileWriter

public class ByteDAO {
	public void fileOpen() {
		try {
			// 파일로부터 byte단위로 데이터를 읽어오고 싶다
			FileInputStream fis = new FileInputStream("a_byte.txt");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	
	public void fileSave() {
		FileOutputStream fos = null;
		try {
			// 파일에 byte단위로 데이터를 작성하고 싶다
			fos = new FileOutputStream("a_byte.txt");
			fos.write(97);
			
			byte[] bArr = {98,99,100,101};
			fos.write(bArr);
			
			fos.write(bArr,1,3);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

new FileOutputStream("a_byte.txt"); 로 생성하고 저장하며

fos.write(97);로 작성하고

new FileInputStream("a_byte.txt");로 불러오며

int data = fis.read();로 세부적인거를 읽어올수있다


▶ 보조 스트림

▶ 보조 스트림 종류

▶ 성능 향상 보조 스트림

▶ 객체 입출력 보조 스트림

▶ 직렬화와 역직렬화

 

public class BufferedDAO {
	public void inputByte() {
		// 파일에 있는 데이터를 바이트 기반으로 빠르게 읽어오고 싶다
		try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream("c_buffer.txt"));) {
			int value;
			while((value = bis.read()) != -1) {
				System.out.print((char)value);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
	}
	
	public void outputByte() {
		// 파일에 바이트 기반으로 데이터를 빠르게 쓰고 싶다
		try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c_buffer.txt"));) {
			bos.write(65);
			byte[] bArr = {66,67,68,69};
			bos.write(bArr);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

	public void outputChar() {
		// 파일에 문자 기반으로 데이터를 빠르게 쓰고 싶다
		try(BufferedWriter bw = new BufferedWriter(new FileWriter("c_buffer.txt"));) {
			bw.write("안녕하세요\n");
			bw.write("반갑습니다\n");
			bw.write("우리또봐요\n");
		} catch (IOException e) {

			e.printStackTrace();
		}
	}
	public void inputChar() {
		// 파일에 문자 기반으로 데이터를 빠르게 읽고 싶다
		// 파일에 있는 데이터를 바이트 기반으로 빠르게 읽어오고 싶다
		try(BufferedReader br = new BufferedReader(new FileReader("c_buffer.txt"));) {
			int value;
			while((value = br.read()) != -1) {
				System.out.print((char)value);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
	}

}

코딩 공부 및 연습 자료

 

바이트 io스트림


문자열io 스트림이다

 

'JAVA' 카테고리의 다른 글

[JAVA] 컬렉션(Collection), 제네릭  (0) 2025.05.01
[JAVA] 예외처리(Exception)  (0) 2025.04.29
[JAVA] 기본 API  (0) 2025.04.28
[JAVA] 다형성(Polymorphism),추상화,인터페이스  (0) 2025.04.28
[JAVA] 상속(Inherit)  (0) 2025.04.25