본문 바로가기

Java

[210831] [exam15] 바이트 기반 파일 입출력

//버퍼드쓰고 싶다면 얘는 bufferedOutputStream, InputStream 
//문자기반일 때가 bufferWriter, -Reader
public class Sample2 {
	//이미지, 비디오, 텍스트 파일을 읽고 쓰기 위해 사용
	//바이트 기반 파일 쓰기 작업
	public static void main(String[] args) {
		try {							//선언하면 자동 트라이 캐치
			//	FileOutputStream fos = new FileOutputStream("./fileoutput.txt");
			File f = new File("./fileoutput.txt");	//파일객체 생성해서 쓰는 방법
			FileOutputStream fos = new FileOutputStream(f, true); 
										//기존 파일 유지 상태로 계속 작성하려면 true 
										//안쓰면 파일 자동 덮어쓰기      [31.19:34] 
			byte[] brr = new byte[] {65, 66, 67, 68, 69};	//txt 파일 확인하면
											//바이트가 영문자 코드로 ABCDE 들어가있음
			fos.write(brr); //(byte[])인걸로 골라야함 파일아웃풋스트림은 바이트타입이니까!
			fos.close(); //클로즈 해줘야함 				//write하면 역시 자동 캐치 
		} catch (FileNotFoundException e) {	//처음 뉴 파일의 파일이 없을 경우 예외처리
			System.out.println("파일 출력 스트림에 문제가 발생하였습니다.");
		} catch (IOException e) {		//파일 쓰기 write에 문제가 생긴 경우 예외처리
			System.out.println("파일 출력 스트림에 문제가 발생하였습니다.");
		}		
	//바이트 기반 파일 읽기 작업
		try {						//여기선()안은 문자열인걸로 선택했음
			//FileInputStream fis = new FileInputStream("./fileoutput.txt");
			File f = new File("./fileoutput.txt");	//파일객체 생성해서 쓰는 방법
			FileInputStream fis = new FileInputStream(f);
			//문자배열에 한번에 넣어서 출력 [31.19:20] 꼭해봐야함 
			char[] text = new char[0];
			while(true) {
				int r = fis.read();
				if(r == -1) {
					break;
				}
				char[] temp = new char[text.length + 1];
				System.arraycopy(text, 0, temp, 0, text.length);
				temp[temp.length - 1] = (char)r;   //형변환해서 문자배열에 넣기!!!!
				text = temp;
			}

			System.out.println(text);
			fis.close();
			
//			while(true) {
//				int r = fis.read();
//				if(r == -1) {	//-1로 하면 다 읽은 것 
//					break;
//				}
//				System.out.printf("%c", r);	//int r을 문자(유니코드)로 출력하기 
//			}	//ex) ("%c" "%d", r, r) //하나 값으로 여러 경우 출력했던거 기억나지?
//read 는 한바이트씩 밖에 안 읽으므로 위에서 while로 출력함 
//			int r1 = fis.read();	//read()반환타입 int 있어서 담음 ->바이트가 인트된것
//			int r2 = fis.read();	//read()반환타입 int 있어서 담음
//			System.out.println(r1 + " | " + r2);	
		} catch (FileNotFoundException e) {
			System.out.println("파일 입력 스트림에 문제가 발생하였습니다.");
		} catch (IOException e) {
			System.out.println("파일 읽기 작업에 문제가 발생하였습니다.");
		}
	}

}

바이트 기반 버퍼 보조 스트림 활용

 

// 바이트 기반 입출력 스트림 + 버퍼 보조 스트림
		try {
			File f = new File("./fileoutput.txt");
			FileOutputStream fos = new FileOutputStream(f);
			BufferedOutputStream bos = new BufferedOutputStream(fos); 
			
			byte[] bArr = new byte[] {65, 66, 67, 68, 69};
			
			bos.write(bArr);
			bos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			System.out.println("파일 출력 스트림에 문제가 발생하였습니다.");
		} catch (IOException e) {
			System.out.println("파일 쓰기 작업에 문제가 발생하였습니다.");
		}
		
		try {
			File f = new File("./fileoutput.txt");
			FileInputStream fis = new FileInputStream(f);
			BufferedInputStream bis = new BufferedInputStream(fis);
			
			char[] text = new char[0];
			while(true) {
				int r = bis.read();
				if(r == -1) {
					break;
				}
				char[] temp = new char[text.length + 1];
				System.arraycopy(text, 0, temp, 0, text.length);
				temp[temp.length - 1] = (char)r;
				text = temp;
			}
			System.out.println(text);
			bis.close();
			fis.close();
		} catch (FileNotFoundException e) {
			System.out.println("파일 입력 스트림에 문제가 발생하였습니다.");
		} catch (IOException e) {
			System.out.println("파일 읽기 작업에 문제가 발생하였습니다.");
		}
	}

'Java' 카테고리의 다른 글

[exam16] 스레드 상속, Runnable 인터페이스  (0) 2021.09.11
[210831] [exam15] 문자 기반 파일 입출력  (0) 2021.09.11
[exam15] try ~ catch문  (0) 2021.09.11
[exam12] 문자열 (String) 관련 메서드  (0) 2021.09.11
[exam14] Map  (0) 2021.09.11