import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class FileEx1 {
public static void main(String[] args){
//생성자에 경로 지정 생성자에 예외처리 해주어야함
FileInputStream fis = null;
try{
fis = new FileInputStream("/home/whitefox/test.txt");
int data = 0;
//문자에 대한 ASCII코드
while((data = fis.read()) != -1){
//ASCII값을 char로 형 변환 엔터키도 코드값으로 변환됨을 확인할 수 있
System.out.print((char)data +" ");
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
// if(fis !=null){;
// try{
// fis.close();
// } catch(IOException e){}
// }
if(fis != null) try{ fis.close(); } catch(IOException e){}
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileEx2 {
public static void main(String[] args){
FileOutputStream fos = null;
try {
// false 덮어쓰기 true 이어쓰기
fos = new FileOutputStream("/home/whitefox/test2.txt", true);
fos.write('a');
fos.write('b');
fos.write('c');
fos.write('\n');
fos.write('1');
fos.write('2');
fos.write('3');
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fos != null) try { fos.close(); } catch(IOException e) {}
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileEx3 {
public static void main(String[] args) throws FileNotFoundException{
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("/home/whitefox/test.txt");
fos = new FileOutputStream("/home/whitefox/test2.txt");
// fis = new FileInputStream("/home/whitefox/Des1.gif");
// fos = new FileOutputStream("/home/whitefox/Desktop/2.gif");
int data = 0;
while((data = fis.read()) != -1){
fos.write(data);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fis != null) try{ fis.close(); } catch(IOException e){}
if(fos != null) try{ fos.close(); } catch(IOException e){}
}
}
}
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileEx4 {
public static void main(String[] args) throws FileNotFoundException{
// FileInputStream fis = null;
BufferedInputStream bis = null;
try {
// fis = new FileInputStream("/home/whitefox/test.txt");
bis = new BufferedInputStream(new FileInputStream("/home/whitefox/test.txt"));
int data = 0;
while((data = bis.read()) != -1){
System.out.print((char)data + " ");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(bis != null) try{ bis.close(); } catch(IOException e){}
// if(fis != null) try{ fis.close(); } catch(IOException e){}
}
}
}
i
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileEx5 {
public static void main(String[] args){
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
// false 덮어쓰기 true 이어쓰기
// fos = new FileOutputStream("/home/whitefox/test2.txt", true);
bos = new BufferedOutputStream(new FileOutputStream("/home/whitefox/test2.txt"));
bos.write('a');
bos.write('b');
bos.write('c');
bos.write('\n');
bos.write('1');
bos.write('2');
bos.write('3');
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
// if(fos != null) try { fos.close(); } catch(IOException e) {}
if(bos != null) try { bos.close(); } catch(IOException e) {}
}
}
}
'Study > Programming' 카테고리의 다른 글
자바 File IO 받은 문자 출력, 파일 생성/삭제/변경 (0) | 2009.05.22 |
---|---|
자바 IO 문자기반 (0) | 2009.05.22 |
자바 swing JTable 사용한 달력 (0) | 2009.05.21 |
자바 Swing 구구단 (0) | 2009.05.21 |
자바 Swing (0) | 2009.05.21 |