입력과 출력
- 자바는 입출력 관련 여러 클래스를 제공해주고 있다.
- 입/출력이란
입력 : 다른 곳의 데이터를 가져오는 것
출력 : 다른 곳으로 데이터를 내보내는 것
스트림 : 데이터가 오고 가는 것, 입출력에 사용되는 장치
스트림 연결을 해주고 read, write 등을 한다
- 입/출력 기본 클래스
입/출력에 사용되는 기본 클래스는 1byte 단위로 데이터를 전송하는
InputStream과 OutputStream이 있다.
스트림을 만들고 input에는 inputStream 클래스 활용, output에는 outputStream을 활용
inputStream, OutputStream을 추상 클래스로 가지고 있다.
InputStream : FileInputStream, DataInputStream, BufferedinputStream
OutputStream : FileOutputStream, DataOutputStream, BufferedOutputStream
- 데코레이터 패턴이 적용된 자바 I/O
1) FileInputStream을 2) BufferedInputStream이 감싸고, 이를 3) LineNumberInpuStream이 감싼다.
FileInputStream : 데이터를 읽어들일 수 있게 해주는 기본 구성요소
BufferedInputStream : 속도를 향상시키기 위해 입력된 내용을 버퍼에 저장한다, 문자로 입력된 내용을 한 번에 한 줄씩 읽어들이기 위한 readLine() 메소드가 들어있는 인터페이스를 구현한다.
LineNumberInputStream : 데이터를 읽을 때 행번호를 붙여주는 기능을 추가해준다.
2), 3)은 FilterInputStream을 확장한 클래스다.
FilterInputStream은 추상 데코레이터 클래스 역할을 한다.
(정리)
InputStream
InputStream을 상속 -> FileInputStream, StringBufferInputStream, ByteArrayInputStream, FilterInputStream
FilterInputStream : 추상 데코레이터
이에 대한 구상 데코레이터 : PushbackInputStream, BufferedInputStream, DataInputStream, LineNumberInputStream
- FileInputStream(read) / FileOutputStream(write)
파일에 데이터를 읽고 쓰기 위한 클래스로 read(), write() 메서드를 이용한다.
read() : 1byte씩 읽고
read(byte[]) : []크기만큼 읽고
write(byte[] b) : 전체 쓰기
write(byte[], int, off, int, len) : off(시작점), len(길이)
public class MainClass {
public static void main(String[] args){
// read()
InputStream inputStream = null;
int data = 0;
byte[] bs = new byte[3];
try {
inputStream = new FileInputStream(경로);
int data = 0;
while (true) {
try {
data = inputStream.read();
// data = inputStream.read(bs);
} catch (IOException e) {
e.printStackTrace();
}
// 더 이상 읽을 게 없으면 -1이 나온다
if (data == -1) break;
}
} catch (FilenotFoundException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null) inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(경로);
String data = "Hello java world!";
byte[] arr = data.getBytes();
try {
outputStream.write(arr,0,5);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
- 파일 복사
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(경로);
outputStream = new FileOutputStream(경로);
byte[] arr = new byte[3];
while (true ) {
int len=inputStream.read(arr);
if (len == -1) break;
outputStream.write(arr,0,len);
}
} catch ( Exception e) {
e.printStackTrace();
} finally {
}
- DataInputStream, DataOutputStream
byte 단위의 입출력을 계산해서 문자열을 좀 더 편리하게 다룰 수 있다.
문자열 단위 처리
OutputStream outputStream = null;
DataOutputStream dataOutputStream = null;
try {
outputStream = new FileOutputStream(경로);
dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeUTF(str);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
- BufferedReader, BufferedWriter
byte 단위의 입출력을 개선해서 문자열을 좀 더 편리하게 다룰 수 있다.
FileReader를 객체를 만들고 BufferedReader의 인자로 준다
FileWriter를 객체를 만들고 BufferedWriter의 인자로 준다