Java: interface for getting lines from a file or line?

I have a method that iterates over the lines of a file. Currently, he is completely dancing, opening the file and closing it.

Now I want to change the method so that I can pass an instance of some interface, it is possible Iterator<String>that I can either read from a file or just get lines from List<String>if I want to provide input directly.

Is there any convenient way to do this? Writing your own method of getting Iterator<String>from Fileseems like it would be very difficult to get the right results.

I think the closest way I can think of is to use Guava Files.readLines () , but this is not an iterator, so it has problems with very large files.

+3
source share
5 answers

As a result, I wrote my own interface and two light classes that implement it, encapsulate the Iterator<String>one that takes its input from the file:

LineReader2.java:

import java.io.Closeable;
import java.io.IOException;

public interface LineReader2 extends Closeable 
{
    void close() throws IOException;
    String readLine() throws IOException; 
}

LineReaders2.java:

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

public class LineReaders2 
{
    private LineReaders2() {}

    static class FileLineReader implements LineReader2 
    {
        final private BufferedReader br;

        public FileLineReader(Reader in)
        {
            this.br = new BufferedReader(in);
        }
        @Override public void close() throws IOException {
            this.br.close();
        }
        @Override public String readLine() throws IOException {
            return this.br.readLine();
        }       
    }

    static class StringIteratorReader implements LineReader2
    {
        final private Iterator<String> it;

        public StringIteratorReader(Iterator<String> it) {
            this.it = it;
        }
        @Override public void close() {}
        @Override public String readLine() {
            return this.it.hasNext() ? this.it.next() : null;
        }       
    }

    static public LineReader2 createReader(File f) throws IOException
    {
        return new FileLineReader(new FileReader(f));
    }
    static public LineReader2 createReader(Iterable<String> iterable)
    {
        return new StringIteratorReader(iterable.iterator());
    }
    static public LineReader2 createReader(Iterator<String> iterator)
    {
        return new StringIteratorReader(iterator);
    }
}
0
source

java.io.Scanner implements Iterator, so I think that is exactly what you want. Write your method to wait for Iterator, and then you can pass the scanner opened in the file, or Iterator from the list of lines.

+4
source

Iterator, BufferedReader ( a FileReader ).

, , hasNext() , BufferedReader.readLine().

, , ( ).

+1

Apache Commons IO has a class LineIterator, From Javadoc:

Recommended usage pattern:

LineIterator it = FileUtils.lineIterator(file, "UTF-8");
try {
  while (it.hasNext()) {
    String line = it.nextLine();
    // do something with line
  }
} finally {
  it.close();
}
+1
source

try something like

interface ISourceOfLines
{
       List<String> getLines();
}

public class FileSource : ISourceOfLines
{
      public FileSource(String filename)
      {
          // store fileName
      }

      public List<String> getLines()
      {
         // open file and return lines
      }
}
0
source

All Articles