简体   繁体   中英

Most efficient way to read characters from a file?

While practising file I/O in Java, I came across an assignment where I has to rewrite a method that looks up what recorddata is associated with a given record ID. Now, the method I'm talking about is using a FileReader wrapped in a BufferedReader in order to read the characters. Oddly enough, the assignment itself suggests that using a BufferedStreamReader(?) might not be the most efficient way of retrieving characters from a file. I find this even more confusing considering the method contains a BufferedReader instead of a BufferedStreamReader.

So my question is, isn't using a BufferedReader wrapper for a FileReader already the most efficient (in terms of speed) way to read characters in a file?

EDIT: The assignment talks of a BufferedStreamReader, not a BufferedInputStream

I haven't come accross the BufferedStreamReader But would read Characters Using BufferedReader First Into String and then Character By character if That is what you are talking about.

            FileInputStream fs = new FileInputStream(filename);
            BufferedReader br = new BufferedReader(new InputStreamReader(fs));
            for (int j = 0; j < 0; j++) {//The the first Line
            String str = br.readLine().trim();
            char[] chars = str.toCharArray();
            String first = String.valueOf(chars[0]);//The first character
            String second = String.valueOf(chars[1]);//The second

            }

A Reader reads characters from a InputStream . Hence it would be the best to buffer the actual file system access, here the BufferedInputStream , because that is what can be slow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM