迭代器用Java读取和处理文件 [英] Iterators to read and process file in Java

查看:362
本文介绍了迭代器用Java读取和处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类 Point 和一个函数来处理 Point 实例

 
class Point {private final int x,y; ...}
...
void handlePoints(Iterable points){for(Point p:points){...}}

现在我想从文件中读取 points 。该文件的每一行都包含两个数字,所以我有一个函数(工厂方法)来创建一个从一个线。

 
Point makePoint(String line){...}

我应该怎么做现在?我可以写一个函数来读取文件到 points 列表并调用 handlePoints 函数。

 
List< Point> readPoints(BufferedReader reader){...} //在这里使用makePoint

void handlePoints(BufferedReader reader){
List< Point> points = readPoints(阅读器);
handlePoints(points);

不幸的是,这个函数看起来并不是特别优雅,因为它在内存中创建了一个不必要的点列表。



使用迭代器会更好吗?

 
void handlePoints(Iterator< Point> points){...}

Iterator< Point> readPoints(BufferedReader reader){...} //在这里使用makePoint

void handlePoints(BufferedReader reader){
Iterator< Point> points = readPoints(阅读器);
handlePoints(points);
}

有意义吗?如果你不需要把所有的点都放在内存中,那么这个代码在Java中是不是太吵闹了?

,请考虑更多的内容:

  while(reader.ready())
{
String line = reader.readLine();
Point point = makePoint(line);
handlePoint(point);





$ b

如何用迭代器和handlePoints来处理异常被添加)

  class PointIterator实现Iterator< Point> 
{
BufferedReader reader;
PointIterator(BufferedReader myReader){reader = myReader; };
@Override
public boolean hasNext(){return myReader.ready(); };
@Override
public Point next(){return makePoint(myReader.readLine()); };
@Override
public void remove()
{throw new UnsupportedOperationException(Remove not supported!); };

code $


$ handlePoints 需要一个 Iterable

  class PointIterable implements Iterable< Point> 
{
BufferedReader reader;
public PointIterable(BufferedReader myReader){reader = myReader; };
@Override
public Iterator< Point> iterator(){返回新的PointIterator(reader); }
}

使用:

  handlePoints(new PointIterable(reader)); 


Suppose I have a class Point and a function to process Point instances

class Point { private final int x, y; ... } 
...
void handlePoints(Iterable<Point> points) { for (Point p: points) {...} }

Now I would like to read points from a file. Each line of the file contains two numbers, so I have a function ("factory method") to create a point from a line.

Point makePoint(String line) { ... }

What should I do now? I can write a function to read the file to a list of points and call the handlePoints function.

List<Point> readPoints(BufferedReader reader) {...} // use makePoint here

void handlePoints(BufferedReader reader) {
   List<Point> points = readPoints(reader); 
   handlePoints(points);
}

Unfortunately this function does not seem particularly elegant since it creates an unnecessary list of points in memory.

Wouldn't it be better to use iterators ?

void handlePoints(Iterator<Point> points) {...}

Iterator<Point> readPoints(BufferedReader reader) {...} // use makePoint here

void handlePoints(BufferedReader reader) {
   Iterator<Point> points = readPoints(reader); 
   handlePoints(points);
}

Does it make sense? Won't be this code too "noisy" in Java?

解决方案

If you don't need to have all points in memory, think of something more along these lines:

while (reader.ready())
{
  String line = reader.readLine();
  Point point = makePoint(line);
  handlePoint(point);
}

How to do this with an iterator and handlePoints: (code for handling exceptions to be added)

class PointIterator implements Iterator<Point>
{
  BufferedReader reader;
  PointIterator(BufferedReader myReader) { reader = myReader; };
  @Override
  public boolean hasNext() { return myReader.ready(); };
  @Override
  public Point next() { return makePoint(myReader.readLine()); };
  @Override
  public void remove()
  { throw new UnsupportedOperationException("Remove not supported!"); };
}

And because handlePoints takes an Iterable:

class PointIterable implements Iterable<Point>
{
  BufferedReader reader;
  public PointIterable(BufferedReader myReader) { reader = myReader; };
  @Override
  public Iterator<Point> iterator() { return new PointIterator(reader); }
}

To use:

handlePoints(new PointIterable(reader));

这篇关于迭代器用Java读取和处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆