마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

입출력 관련해서 궁금한게 있습니다.

0 추천
버퍼에 관해서 궁금한게 있는데요..

어떤 파일에 있는 것을 읽고 다시 다른곳에 쓴다고 할때.

FileInputStream fis;

BufferedInputStream bis;

위의 두가지를 사용하여 파일을 읽는데요.

그런데

 

byte[] buffer = new byte[66536];

while((len = fis.read(buffer))!=-1){

}

위 처럼 바이트배열을 버퍼로 사용하는 경우도 있는데요.

 

첫번째 방법과 두번째 방법의 차이점이 무엇인지 궁금합니다.

버퍼스트림(BufferedInputStream)을 사용하면 아래 방법처럼 굳이 바이트배열을 버퍼로 사용하지 않아도 되는건가요?

궁금합니다!
갸아악 (21,260 포인트) 님이 2014년 7월 28일 질문

1개의 답변

+1 추천
 
채택된 답변

http://docs.oracle.com/javase/7/docs/api/index.html 찾아보시면 나옵니다.

 

BufferInputStream은 또 다른 InputStream에서 읽어오고, (byte 쓰긴씁니다)

FileInputStream은 바로 읽는것 같습니다.

 

BufferInputStream 은 FIS의 도우미 역활같은데(성능 업을 위한)...확실친 못하겠네요.

 

결론은.. BufferInputStream을 FileInputStream과 같이쓰면 성능 향상이 된다(?)

 

참고: http://i5on9i.egloos.com/4840636

참고: http://stackoverflow.com/questions/21641551/what-is-the-difference-between-fileinputstream-and-bufferedinputstream-in-java

참고: http://hyeonstorage.tistory.com/238

참고: http://stackoverflow.com/questions/18600331/why-is-using-bufferedinputstream-to-read-a-file-byte-by-byte-faster-than-using-f

참고: http://examples.javacodegeeks.com/

public class BufferedInputStreamExample {

    public static void main(String[] args) {

        File file = new File("inputfile.txt");

        BufferedInputStream bin = null;

        FileInputStream fin = null;

        try {
            // create FileInputStream object
            fin = new FileInputStream(file);

            // create object of BufferedInputStream
            bin = new BufferedInputStream(fin);

            // byte array to store input
            byte[] contents = new byte[1024];

            int bytesRead=0;

            String s;

            while ((bytesRead = bin.read(contents)) != -1) {
                s = new String(contents, 0, bytesRead);
                System.out.print(s);
            }
        }
        catch (FileNotFoundException e) {
            System.out.println("File not found" + e);
        }
        catch (IOException ioe) {
            System.out.println("Exception while reading file " + ioe);
        }
        finally {
            // close the streams using close method
            try {
                if (fin != null) {
                    fin.close();
                }
                if (bin != null) {
                    bin.close();
                }
            }
            catch (IOException ioe) {
                System.out.println("Error while closing stream : " + ioe);
            }
        }
    }
}
public class FileInputStreamExample {

    public static void main(String[] args) {

        File file = new File("inputfile.txt");
        FileInputStream fin = null;

        try {
            // create FileInputStream object

            fin = new FileInputStream(file);

            byte fileContent[] = new byte[(int)file.length()];
 
            // Reads up to certain bytes of data from this input stream into an array of bytes.
            fin.read(fileContent);
            //create string from byte array
            String s = new String(fileContent);
            System.out.println("File content: " + s);
        }
        catch (FileNotFoundException e) {
            System.out.println("File not found" + e);
        }
        catch (IOException ioe) {
            System.out.println("Exception while reading file " + ioe);
        }
        finally {
            // close the streams using close method
            try {
                if (fin != null) {
                    fin.close();
                }
            }
            catch (IOException ioe) {
                System.out.println("Error while closing stream: " + ioe);
            }
        }
    }
}

 

익명사용자 님이 2014년 7월 28일 답변
갸아악님이 2014년 7월 28일 채택됨
...