java FileInputStream 과 FileOutputStream 깊이 분석

7810 단어 자바
FileInputStream 과 FileOutputStream 류 는 바이트 류 에 속 하 며 임의의 형식의 파일 을 조작 할 수 있 습 니 다.데이터 흐름 의 처리 과정 에서 두 가지 상황 이 있다.(1) 단일 바이트 형식 으로 파일 을 읽 고 쓰기 (2) 데이터 블록 형식 으로 파일 을 읽 고 쓰 는 것 은 JDK 의 소스 코드 에서 알 수 있 습 니 다. FileInputStream 의 읽 기:
public native int read() throws IOException;
private native int readBytes(byte b[], int off, int len) throws IOException;

FileOutputStream 의 쓰기:
public native void write(int b) throws IOException;
private native void writeBytes(byte b[], int off, int len) throws IOException;

FileInputStream 과 FileOutputStream 에서 가장 많이 사용 되 는 곳 은 바로 파일 의 복사 과정 이다.다음은 두 가지 예 를 통 해 설명 하 겠 습 니 다. 예 1: 한 바이트 의 읽 기와 쓰기.
public class Test
{
    public static void main(String[] args)
    {
        File src = new File("d:\\src.txt");
        File dst = new File("d:\\dst.txt");
        doSaveFile(src, dst);
    }
    public static void doSaveFile(File src, File dst)
    {
        InputStream in = null;
        OutputStream out = null;
        try
        {
            in = new FileInputStream(src);
            out = new FileOutputStream(dst);
            int len = 0;
            while ((len = in.read()) > 0)
            {//len       
                out.write(len);
            }
        }
        catch (Exception e)
        {
        }
        finally
        {
            if (null != in)
            {
                try
                {
                    in.close();
                }
                catch (IOException e)
                {
                }
            }
            if (null != out)
            {
                try
                {
                    out.close();
                }
                catch (IOException e)
                {
                }
            }
        }
    }
}

예 2: 데이터 블록의 읽 기와 쓰기
public class Test
{
    private final static int BUFFER_SIZE = 16 * 1024;
    public static void main(String[] args)
    {
        File src = new File("d:\\src.txt");
        File dst = new File("d:\\dst.txt");
        doSaveFile(src, dst);
    }
    public static void doSaveFile(File src, File dst)
    {
        InputStream in = null;
        OutputStream out = null;
        try
        {
            in = new FileInputStream(src);
            out = new FileOutputStream(dst);
            byte[] buffer = new byte[BUFFER_SIZE];
            int len = 0;
            while ((len = in.read(buffer)) > 0)
            {//len        
                out.write(buffer, 0, len);
            }
        }
        catch (Exception e)
        {
        }
        finally
        {
            if (null != in)
            {
                try
                {
                    in.close();
                }
                catch (IOException e)
                {
                }
            }
            if (null != out)
            {
                try
                {
                    out.close();
                }
                catch (IOException e)
                {
                }
            }
        }
    }
}

의문: 위 에 두 가지 예 가 열거 되 어 있 는데 도대체 어떤 예 가 효율 이 더 높 습 니까?효율 적 인 비교 가 쉽 지 않 기 때문에 이 문 제 는 대답 하기 어 려 울 지도 모른다.그리고 데이터 흐름 의 밑바닥 이 실현 되 는 것 도 우리 가 알 기 어렵다.사실 이 문 제 를 떠 나 우 리 는 더 효율 적 인 읽 기 방식 이 있다. 그것 이 바로 다음 글 에서 말 하고 자 하 는 Buffered InputStream 과 Buffered OutputStream 이다.주의해 야 할 것 은 FileInputStream 인 스 턴 스 대상 을 만 들 때 지정 한 파일 은 존재 하고 읽 을 수 있어 야 합 니 다.FileOutputStream 인 스 턴 스 대상 을 만 들 때 지정 한 파일 이 존재 하면 이 파일 의 원래 내용 은 덮어 쓰 고 지 워 집 니 다.
다음으로 이동: FileInputStream 과 FileOutputStream 깊이 분석

좋은 웹페이지 즐겨찾기