본문 바로가기
JAVA

[JAVA] 입출력(I/O) Stream - 3 (보조스트림)

by happenstance 2021. 8. 6.

용량이 큰 파일을 입출력 할 때는 버퍼를 사용하는 것이 효율적이다.

 

 

🌳 바이트 기반의 버퍼 스트림 사용하기

public class BufferedIOTest01 {

	public static void main(String[] args) {
		try {
			FileOutputStream ouptut = 
					new FileOutputStream("d:/d_other/bufferTest.txt");
			
			// 버퍼의 크기가 5인 (보조)버퍼스트림 객체 생성
			//  => 버퍼의 크기를 지정하지 않으면 기본 크기인 8Kb(8196byte)로 지정된다.
			BufferedOutputStream bufOutput =
					new BufferedOutputStream(ouptut, 5);
			
			System.out.println("작업 시작");
			
			for (int i = '1'; i <= '9'; i++) {
				bufOutput.write(i);
			}
			bufOutput.flush(); // 버퍼에 남아있는 데이터를 모두 가제로 출력한다.
			
			System.out.println("작업 끝");
			
			bufOutput.close(); // 보조스트림을 닫으면 기반스트림도 닫힌다.
				     // 버퍼스트림의 close()에는 flush() 기능도 있다.
		} catch (IOException e) {
		}
	}
}

 

 

🌳 문자 기반의 버퍼 스트림 사용하기

public class BufferedIOTest02 {

	public static void main(String[] args) {
		try {
			// 이클립스에서 자바 프로그램이 실행되는 현재 위치는
			// 해당 프로젝트 폴더가 현재 위치가 된다.
			FileReader fr = new FileReader("./src/basic/FileTest01.java");
			BufferedReader br = new BufferedReader(fr);

			String temp = ""; // 읽어온 데이터가 저장될 변수
			// 문자 기반의 버퍼 메소드 중 readLine() 메소드는 한 줄 단위로 읽어온다.
			for (int i = 1; (temp = br.readLine()) != null; i++) {
				System.out.printf("%4d : %s\n", i, temp);
			}
			br.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

🍂 실행 결과 (콘솔창)

   1 : package 패키지명;
   2 : 
   3 : import java.io.File;
   4 : 
   5 : public class FileTest01 {
   6 : 	public static void main(String[] args) {
   7 : 		// File 객체 만들기 연습
   8 : 		// 1. new File(String 파일 또는 경로)
   9 : 		// ==> 디렉토리와 디렉토리 사이 또는 디렉토리와 파일 사이의
  10 : 		// 구분 문자는 '\'를 사용하거나 '/'를 사용할 수 있다.
  11 : //		File file1 = new File("d:\\d_other\\test.txt"); // 구분 문자로 '\'를 사용
  12 : 		File file1 = new File("d:/d_other/test.txt"); // 구분 문자로 '\'를 사용
  13 : 
  14 : 		System.out.println("파일명 : " + file1.getName());
  15 : 		System.out.println("디렉토리일까? : " + file1.isDirectory());
  16 : 		System.out.println("파일일까? : " + file1.isFile());
  17 : 		System.out.println();
  18 : 
  19 : 		File file2 = new File("d:/d_other");
  20 : 		System.out.println("파일명 : " + file2.getName());
  21 : 		System.out.println("디렉토리일까? : " + file2.isDirectory());
  22 : 		System.out.println("파일일까? : " + file2.isFile());
  23 : 		System.out.println();
  24 : 
  25 : 		// 2. new File(File parent, String child)
  26 : 		// ==> 'parent' 디렉토리 안에 있는 'child' 파일을 갖는다.
  27 : 		File file3 = new File(file2, "test.txt");
  28 : 		System.out.println("파일명 : " + file3.getName());
  29 : 		System.out.println("디렉토리일까? : " + file3.isDirectory());
  30 : 		System.out.println("파일일까? : " + file3.isFile());
  31 : 		System.out.println();
  32 : 
  33 : 		// 3. new File(String parent, String child)
  34 : 		// ==> 'parent' 디렉토리 안에 있는 'child' 파일을 갖는다.
  35 : 		File file4 = new File("d:/d_other", "test.txt");
  36 : 		System.out.println("파일명 : " + file4.getName());
  37 : 		System.out.println("디렉토리일까? : " + file4.isDirectory());
  38 : 		System.out.println("파일일까? : " + file4.isFile());
  39 : 		System.out.println();
  40 : 
  41 : 		// 디렉토리(폴더) 만들기
  42 : 		// - mkdir() ==> File 객체의 경로 중에서 제일 마지막 위치의 디렉토리를 만든다.
  43 : 		// 			 ==> 반환값 : 만들기 성공: true, 실패: false
  44 : 		//			 ==> 지정한 경로 중 중간 부분의 경로가 모두 만들어져 있어야
  45 : 		//				 마지막 위치의 디렉토리를 만들 수 있다.
  46 : 		
  47 : 		// - mkdirs() ==> File 객체의 경로 중에서 제일 마지막 위치의 디렉토리를 만든다.
  48 : 		//  		  ==> 반환값 : 만들기 성공: true, 실패: false
  49 : 		//   		  ==> 중간 부분의 경로가 없으면 중간 부분의 경로도 함께 생성한다.
  50 : 		File file5 = new File("d:/d_other/연습용");
  51 : 		System.out.println(file5.getName() + "의 존재 여부 : " + file5.exists());
  52 : 		if (!file5.exists()) {
  53 : 			if (file5.mkdir()) {
  54 : 				System.out.println(file5.getName() + " 만들기 성공");
  55 : 			} else {
  56 : 				System.out.println(file5.getName() + " 만들기 실패");
  57 : 			}
  58 : 		}
  59 : 		System.out.println();
  60 : 		
  61 : 		// mkdir()은 상위의 모든 경로가 생성되어 있어야 한다.
  62 : 		File file6 = new File("d:/d_other/test/java/src");
  63 : 		if (!file6.exists()) {
  64 : 			if (file6.mkdirs()) {
  65 : 				System.out.println(file6.getName() + " 만들기 성공");
  66 : 			} else {
  67 : 				System.out.println(file6.getName() + " 만들기 실패");
  68 : 			}
  69 : 		}
  70 : 	}
  71 : }
  72 : 
  73 : // 실행 결과
  74 : // 파일명 : test.txt
  75 : // 디렉토리일까? : false
  76 : // 파일일까? : true
  77 : 
  78 : // 파일명 : d_other
  79 : // 디렉토리일까? : true
  80 : // 파일일까? : false
  81 : 
  82 : // 파일명 : test.txt
  83 : // 디렉토리일까? : false
  84 : // 파일일까? : true
  85 : 
  86 : // 파일명 : test.txt
  87 : // 디렉토리일까? : false
  88 : // 파일일까? : true
  89 : 
  90 : // 연습용의 존재 여부 : false
  91 : // 연습용 만들기 성공

 

'JAVA' 카테고리의 다른 글

[JAVA] 입출력(I/O) Stream - 2  (0) 2021.08.05
[JAVA] 입출력(I/O) - 1  (0) 2021.08.04
[JAVA] 스레드의 동기화 - 2  (0) 2021.08.04
[JAVA] 스레드의 동기화 - 1  (0) 2021.08.03
[JAVA] 멀티 스레드 - 2  (0) 2021.07.30