Java에서 입출력(IO)을 위한 인터페이스와 클래스들
물이 나오는(흐르는) 호스에 '어떤 수도 꼭지를 달까? (장식할까?)'를 생각해보자!
public class ByteIOExam1 {
public static void main(String[] args){
FileInputStream fis = null; // 읽기
FileOutputStream fos = null; // 쓰기
try {
fis = new FileInputStream("src/javaIO/exam/ByteExam1.java");
fos = new FileOutputStream("byte.txt");
// 읽어들일때 while read가 핵심! 한 바이트 읽고 가져오기
int readData = -1;
while((readData = fis.read())!= -1){
fos.write(readData);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
// close할때도 error catch 해줘야한다 ㅎ
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
위 코드를 러닝하면 byte.txt 파일로 결과를 확인 가능하다!
public class ByteIOExam1 {
public static void main(String[] args){
//메소드가 시작된 시간을 구하기 위함
long startTime = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("src/javaIO/exam/ByteExam1.java");
fos = new FileOutputStream("byte.txt");
int readCount = -1;
byte[] buffer = new byte[512];
while((readCount = fis.read(buffer))!= -1){
fos.write(buffer,0,readCount);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//메소드가 끝났을때 시간을 구하기 위함.
long endTime = System.currentTimeMillis();
//메소드를 수행하는데 걸린 시간을 구할 수 있음.
System.out.println(endTime-startTime);
}
}
read를 통해 기본적으로 String, 즉 문자로 읽는 것은 알겠다,, 근데 String이 아니라 다양한 Data Type으로 읽고 써야 한다면??
try(
// 괄호 부분에 io객체 선언
// DataOutputStream은 다양한 타입으로 object 저장 가능!
) {
//io객체 사용
}catch(Exception ex){
ex.printStackTrace();
}
import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class ByteExam3 {
public static void main(String[] args) {
try(
DataOutputStream out = new DataOutputStream(new FileOutputStream("data.txt"));
){
// 실제 사용할 코드! -> write를 다양하게 오버라이딩하고 있다!
out.writeInt(100); // 4byte 저장
out.writeBoolean(true); // 1byte 저장
out.writeDouble(50.5); // 8byte 저장
}catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.DataInputStream;
import java.io.FileInputStream;
public class ByteIOExam4 {
public static void main(String[] args) {
try(
DataInputStream out = new DataInputStream(new FileInputStream("data.dat"));
){
int i = out.readInt();
boolean b = out.readBoolean();
double d = out.readDouble();
System.out.println(i);
System.out.println(b);
System.out.println(d);
}catch(Exception ex){
ex.printStackTrace();
}
}
}
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class CharIOExam01 {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//키보드로 입력받은 문자열을 저장하기 위해 line변수를 선언
String line = null;
try {
line = br.readLine()
} catch (IOException e) {
e.printStackTrace();
}
//콘솔에 출력
System.out.println(line);
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class CharIOExam02 {
public static void main(String[] args) {
// 제대로 error catch를 하기 위해 선언부와 입력부를 따로!
BufferedReader br = null; // 입력을 위해
PrintWriter pw = null; // 출력을 위해
try{
br = new BufferedReader(new FileReader("src/javaIO/exam/CharIOExam02.java"));
pw = new PrintWriter(new FileWriter("test.txt"));
String line = null;
// 여러줄 읽기위해 반복문
while((line = br.readLine())!= null){
pw.println(line);
}
}catch(Exception e){
// file not found error catch!
e.printStackTrace();
}finally {
pw.close();
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}