1) DB 접속
2) Networking
3) I/O : 하드디스크에서 파일 읽어오기
4) 쓰레드
1) 예외가 발생 하거나, 안하거나 무조건 수행하는 예제
2) Exception 클래스는 모든 예외 클래스의 최상위 클래스 이므로 가장 마지막 블록에 위치 해야 함
package com.busan.fullstack.stream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class StreamTest {
int temp;
public void test() {
FileReader fi= null;
try {
// FileInputStream fi = new FileInputStream("c:/human/a.txt");
fi = new FileReader("c:/human/a.txt");
while((temp=fi.read())!=-1) {
System.out.print((char)temp);
}
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
fi.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
public void test1() {
FileReader fi= null;
FileWriter fw = null;
try {
fi = new FileReader("c:/human/a.txt");
fw= new FileWriter("c:/human/b.txt");
while((temp=fi.read())!=-1) {
fw.write(temp);
}
}catch (Exception e) {
e.printStackTrace();
}finally {
try {
fi.close();
fw.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
//3. BufferedReader, BufferedWriter 사용
public void test2(){
String tempStr;
try{
BufferedReader br = new BufferedReader(new FileReader("c:/human/a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("c:/human/test.txt"));
while((tempStr=br.readLine())!=null){
System.out.println(tempStr);
bw.write(tempStr+"\r\n");
}
br.close();
bw.close();
}catch(Exception e){
e.printStackTrace();
}
}
//jdk 7부터 사용 가능
public void test3() throws Exception{
String tempStr;
BufferedReader br=new BufferedReader(new FileReader("c:/human/a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("c:/human/test4.txt"));
// try(BufferedReader br=new BufferedReader(new FileReader("c:/human/a.txt"));
// BufferedWriter bw = new BufferedWriter(new FileWriter("c:/human/test3.txt"))){
try(br;bw) {
while((tempStr=br.readLine())!=null){
System.out.println(tempStr);
bw.write(tempStr+"\r\n");
}
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
StreamTest st = new StreamTest();
st.test3();
}
}