일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 가상요소 선택자
- 상속
- datalist
- 서블릿
- ES6
- jsp
- vmax
- nth-of-type()
- optgroup
- EDWITH
- JavaScript
- 복합 선택자
- 즐거운자바
- vmin
- spring
- 김성박
- 부정 선택자
- 웹개발
- 자바
- 속성 선택자
- 즐거운 자바
- 부스트코스
- 깃헙
- 보충필요
- 가상클래스 선택자
- HTML Templating
- 소스트리
- 다시보자
- 다시볼 것
- nth-child()
Archives
- Today
- Total
기억의 DataBase
I/O(Input / Output) 본문
-
Java I/O의 기본 개념
1. 자바 IO는 기본적으로 데코레이터 패턴을 사용
> 주인공과 장식이라는 구분을 가짐
> 다양한 방법으로 읽고, 쓰기 위해서 조립식으로 설계
2. 표준입력 : System.in(키보드, InputStream)
표준출력 : System.out(모니터, PrintStream)
표준에러출력 : System.err(모니터, PrintStream)
3. 자바 IO는 바이트(1 byte) 입출력과 문자(2 byte) 입출력이 있음
바이트 입출력 : 클래스 이름이 Stream으로 끝남
입력 - 000InputStream으로 끝남
출력 - 000OutputStream으로 끝남
문자 입출력 : 클래스 이름이 Reader, Writer로 끝남
입력 - 000Reader로 끝남
출력 - 000Writer로 끝남
4. 자바 IO는 추상클래스인 InputStream, OutputStream, Reader, Writer 중에
하나를 상속 받음
하나를 상속 받음
ex) new InputStream (X) - 인스턴스화 불가
5. 장식에 해당하는 클래스는 생성자에 InputStream, OutputStream, Reader, Writer
클래스를 파라미터로 받아들임
클래스를 파라미터로 받아들임
(주인공 - 생성자를 보면 InputStream, OutputStream, Reader, Writer를
받아들이지 않는 클래스)
받아들이지 않는 클래스)
> 생성자에 추상클래스를 받아들인다는 것은, 그 추상클래스의 자손이나 후손의
인스턴스를 받아 들인다는 의미
인스턴스를 받아 들인다는 의미
※ 생성자에 파라미터를 준다는 것은 해당 클래스에게 파라미터를 가지라는 의미
ex) new 자동차(v3 엔진)
-
주인공과 장식
주인공 - 간단한 메소드만 가지고 있음(읽고 쓴다)
> 한 바이트씩 읽어들여, 한 바이트씩 쓰기
ex) int read()
int read(byte[])
write(int)
write(byte[])
장식 - 다양한 메소드를 가지고 있음(한줄씩 읽고 쓴다 / int 단위로 읽고 쓴다)
> int, char, long으로 읽어들인 다음 쓰시오(주인공 만으로는 어려움)
//byte[]로 읽어들여서, 파일에 저장
public class IO {
public static void main(String[] args) throws Exception {
InputStream in = null;
OutputStream out = null;
try {
byte[] buffer = new byte[]{1, 2, 3};
in = new ByteArrayInputStream(buffer); // 주인공
out = new FileOutputStream("ByteArray.dat"); // 주인공
int readBuffer = -1; // 끝을 지정
while((readBuffer = in.read()) != -1){ // read() 간단
out.write(readBuffer); // write() 간단
}
}catch(Exception ex){
ex.printStackTrace();
}finally {
out.close();
in.close();
}
}
}
// 파일로 byte배열을 읽어들인 후, 배열에 저장
public class IO2 {
public static void main(String[] args) throws Exception {
InputStream in = null;
ByteArrayOutputStream out = null;
try {
in = new FileInputStream("ByteArray.dat");
out = new ByteArrayOutputStream();
int readBuffer = -1; // 끝을 지정
while((readBuffer = in.read()) != -1){ //
out.write(readBuffer);
}
byte[] buf = out.toByteArray();
for(int i= 0 ; i< buf.length;i++) {
System.out.println(buf[i]);
}
}catch(Exception ex){
ex.printStackTrace();
}finally {
out.close();
in.close();
}
}
}
// 파일로 읽어서, 파일로 씀
public class IO3 {
public static void main(String[] args) throws Exception {
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream("./src/Day0121/IO3.java");
out = new FileOutputStream("file.txt");
int readCount = 0;
byte[] buffer = new byte[1024];
while((readCount = in.read(buffer)) != -1){
out.write(buffer, 0, readCount);
}
}catch(Exception ex){
ex.printStackTrace();
}finally {
out.close();
in.close();
}
}
}
// URL을 읽어서, 파일로 씀
public class IO4 {
public static void main(String[] args) throws Exception {
InputStream in = null;
OutputStream out = null;
try{
in = url.openStream();
out = new FileOutputStream("overView.txt");
int readCount = 0;
byte[] buffer = new byte[1024];
while((readCount = in.read(buffer)) != -1){
out.write(buffer, 0, readCount);
}
}catch(Exception ex){
ex.printStackTrace();
}finally {
out.close();
in.close();
}
}
}
-
키보드로 읽어 들여서, 파일로 저장
키보드로 입력을 받음 - System.in(InputStream)
한줄 읽어들임 - BufferedReader / readLine()
파일에 씀 - FileWriter
한줄 씀 - PrintWriter
// 키보드로 읽어 들여서, 파일로 저장
public class IO5 {
public static void main(String[] args) {
BufferedReader br = null; // 한줄씩 입력 받아서
PrintWriter pw = null; // 한줄씩 씀
try {
// 키보드로부터 입력받도록 br을 초기화
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
// 파일에 쓰도록 pw를 초기화
FileWriter fw = new FileWriter("keyboard.txt");
pw = new PrintWriter(fw);
String line = null;
int num = 1;
while((line = br.readLine()) != null){
pw.println(num+":"+ line);
num++; // 입력 종료 : ctr + z
}
}catch(Exception ex){
ex.printStackTrace();
}finally{
try {
pw.close();
}catch(Exception ex) {}
try {
br.close();
}catch(Exception ex) {}
}
}
}
-
정수, 실수, boolean 값을, 파일에 저장
byte, char, byte[], char[] >> 읽고 쓰는 것은 주인공
DataOutputStream >> 정수, 실수 , boolean 값을 저장
FileOutputStream >> 파일에 저장
public class IO6 {
public static void main(String[] args) {
DataOutputStream out = null;
try {
int i = 5;
double d = 20.5;
boolean b = true;
FileOutputStream fos = new FileOutputStream("data.dat");
out = new DataOutputStream(fos);
out.writeInt(i);
out.writeDouble(d);
out.writeBoolean(b);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
out.close();
} catch (Exception ex) {
}
}
}
}
-
정수, 실수, boolean 값을 파일에서 읽어들이시오
DataInputStream >> 정수, 실수 , boolean 값을 읽어들임
FileInputStream >> 파일에서 읽어들임
public class IO7 {
public static void main(String[] args) {
DataInputStream in = null;
try {
in = new DataInputStream(new FileInputStream("data.dat"));
int i = in.readInt();
double d= in.readDouble();
boolean b= in.readBoolean();
System.out.println(i);
System.out.println(d);
System.out.println(b);
}catch(Exception ex) {
ex.printStackTrace();
}finally{
try {
in.close();
}catch(Exception ex) {}
}
}
}
※ System.currentTimeMillis() << System.nanoTime() - 더 정밀함
Comments