Skip to content

Commit

Permalink
add: Files类支持按行读取的API
Browse files Browse the repository at this point in the history
  • Loading branch information
wendal committed Sep 22, 2013
1 parent 0743627 commit 08ce38d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/org/nutz/lang/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
Expand All @@ -18,6 +19,7 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.nutz.lang.util.Callback;
import org.nutz.lang.util.ClassTools;
import org.nutz.lang.util.Disks;

Expand Down Expand Up @@ -989,4 +991,33 @@ else if (flag)
public static boolean copyOnWrite(String path, Object obj) {
return copyOnWrite(new File(path), obj);
}

public static List<String> readLines(File f) {
List<String> lines = new ArrayList<String>();
BufferedReader br = null;
try {
br = Streams.buffr(Streams.fileInr(f));
while (br.ready())
lines.add(br.readLine());
} catch (IOException e) {
throw Lang.wrapThrow(e);
} finally {
Streams.safeClose(br);
}
return lines;
}

public static void readLine(File f, Callback<String> callback) {
BufferedReader br = null;
try {
br = Streams.buffr(Streams.fileInr(f));
while (br.ready())
callback.invoke(br.readLine());
} catch (ExitLoop e) {
} catch (IOException e) {
throw Lang.wrapThrow(e);
} finally {
Streams.safeClose(br);
}
}
}

0 comments on commit 08ce38d

Please sign in to comment.