码迷,mamicode.com
首页 > 其他好文 > 详细

An implementation of this using bufferedReader:

时间:2017-12-01 23:29:10      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:lin   div   file   nta   har   buffered   port   read   over   

 

public class TextFile implements Iterable<String>{

        private BufferedReader br;

        public TextFile(String fileName) throws FileNotFoundException {
            br = new BufferedReader(new FileReader(fileName));
        }

        @Override
        public Iterator<String> iterator() {
                return new Iterator<String>() {
            @Override
            public boolean hasNext() {
                try {
                    br.mark(1);
                    if (br.read() < 0) {
                        return false;
                    }
                    br.reset();
                    return true;
                } catch (IOException e) {
                    return false;
                }
            }

            @Override
            public String next() {
                try {
                    return br.readLine();
                } catch (IOException e) {
                    return null;
                }
            }
            @Override
            public void remove() {
                throw new UnsupportedOperationException();
            }

        };
        }
}

  

An implementation of this using bufferedReader:

标签:lin   div   file   nta   har   buffered   port   read   over   

原文地址:http://www.cnblogs.com/apanda009/p/7944631.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!