[Java] FileInputStream, FileOutputStream
package io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class InOut4
{
/**
* @param args
*/
public static void main(String[] args)
{
// \: escape 문자 \n \r \t \\ : 문자\
FileInputStream fis =null;
FileOutputStream fos =null;
try
{
fis = new FileInputStream("c:\\1.java");
fos = new FileOutputStream("c:/2.java",false); // /<이거 가능
//파일이 없으면 생성 파일이 있으면 기존 파일 사라진다.
//주위 이미 있는 파일이면 확인 안하고 덮어씌운다.
//ture : 덮 붙이기, false : 덮어씌우기
//파일스트림은 끝이 있다.
byte [] b = new byte[512];
int n = 0;
// read(byte[])를 쓰면 파일의 끝에 다다르면 -1리턴
while((n=fis.read(b)) !=-1)
{
fos.write(b,0,n);
System.out.write(b, 0, n);
}
//while(true); // <웹처럼 버퍼가 물고 있다.
//그렇기 때문에 close해야 된다.
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally{
try
{
if(fis != null) fis.close(); //버퍼에도 흐름이 있어 프로그램이 계속 되기 떄문에 close();해줘야 한다.
if(fos != null) fos.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
[출처] http://blog.naver.com/jsh7520501?Redirect=Log&logNo=130081802739