Java Text File Read Line readlines(String filename)

Here you can find the source of readlines(String filename)

Description

readlines

License

Open Source License

Declaration

public static Iterable<String> readlines(String filename) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

public class Main {
    public static Iterable<String> readlines(String filename) throws IOException {

        final FileReader fr = new FileReader(filename);
        final BufferedReader br = new BufferedReader(fr);

        return new Iterable<String>() {
            public Iterator<String> iterator() {
                return new Iterator<String>() {
                    public boolean hasNext() {
                        return line != null;
                    }/* w w  w  .j a  va  2s.c o  m*/

                    public String next() {
                        String retval = line;
                        line = getLine();
                        return retval;
                    }

                    public void remove() {
                        throw new UnsupportedOperationException();
                    }

                    String getLine() {
                        String line = null;
                        try {
                            line = br.readLine();
                        } catch (IOException ioEx) {
                            line = null;
                        }
                        return line;
                    }

                    String line = getLine();
                };
            }
        };
    }
}

Related

  1. readLines(String file)
  2. readLines(String file)
  3. readLines(String file, ArrayList lines)
  4. readLines(String filename)
  5. readLines(String fileName)
  6. readLines(String filename)
  7. readLines(String filename)
  8. readLines(String filename)
  9. readLines(String fileName)