import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileEx6 {
public static void main(String[] args){
FileReader fr = null;
try {
fr = new FileReader("/home/whitefox/test.txt");
int data = 0;
// -1(마지막 값이 아니면 (char)data 실행 : 한글도 정상적으로 출력됨
//Byte기반과는 달리 이미지는 깨지는 경우도 발생
while((data = fr.read()) != -1){
System.out.print((char)data +" ");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(fr != null) try { fr.close(); } catch(IOException e) {}
}
}
}
import java.io.FileWriter;
import java.io.IOException;
public class FileEx7 {
public static void main(String[] args){
FileWriter fw = null;
try {
fw = new FileWriter("/home/whitefox/test2.txt");
//String이 들어감(Byte 기반은 int)
fw.write("가나다라마바");
fw.write("\n"); //자동으로 Enter 안들어감
fw.write("123456");
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fw != null) try { fw.close(); } catch(IOException e) {}
}
}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileEx8 {
public static void main(String[] args){
// FileReader fr = null;
BufferedReader br = null;
try {
// fr = new FileReader("/home/whitefox/test.txt");
br = new BufferedReader(new FileReader("/home/whitefox/test.txt"));
int data = 0;
while((data = br.read()) != -1){
System.out.print((char)data +" ");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
// if(fr != null) try { fr.close(); } catch(IOException e) {}
if(br != null) try { br.close(); } catch(IOException e) {}
}
}
}
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class FileEx8 {
public static void main(String[] args){
// FileReader fr = null;
BufferedReader br = null;
try {
// fr = new FileReader("/home/whitefox/test.txt");
br = new BufferedReader(new FileReader("/home/whitefox/test.txt"));
// int data = 0;
//
// while((data = br.read()) != -1){
// System.out.print((char)data +" ");
// }
//검사해주는 기법이 틀림
String data = null;
while((data = br.readLine()) != null){
//Enter를 표현하기 위해 print가 아닌 println로 표현
System.out.println(data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
// if(fr != null) try { fr.close(); } catch(IOException e) {}
if(br != null) try { br.close(); } catch(IOException e) {}
}
}
}
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class FileEx9 {
public static void main(String[] args){
// FileWriter fw = null;
BufferedWriter bw = null;
System.out.println(bw);
try {
// fw = new FileWriter("/home/whitefox/test2.txt");
bw = new BufferedWriter(new FileWriter("/home/whitefox/test2.txt"));
bw.write("가나다라마바");
bw.write("\n");
bw.write("123456");
} catch (IOException e) {
e.printStackTrace();
} finally{
System.out.println(bw);
// if(fw != null) try { fw.close(); } catch(IOException e) {}
if(bw != null) try { bw.close(); } catch(IOException e) {}
}
}
}
'Study > Programming' 카테고리의 다른 글
자바 Swing 달력 (0) | 2009.05.22 |
---|---|
자바 File IO 받은 문자 출력, 파일 생성/삭제/변경 (0) | 2009.05.22 |
자바 IO 바이트기반 (0) | 2009.05.22 |
자바 swing JTable 사용한 달력 (0) | 2009.05.21 |
자바 Swing 구구단 (0) | 2009.05.21 |