【发布时间】:2015-11-09 22:46:33
【问题描述】:
我有一个场景,我将获得一个大数据作为输入流,它将有一个分隔符并将其拆分并处理它们。如果可能的话,我想完全在内存中处理。现在我在扫描仪的帮助下实现了这一点,如下所示,在代码中:
package chap5_questions;
import java.util.Scanner;
public class paintjob_chp5 {
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class ScannerTest {
public static void main(String[] args) {
FileInputStream fin = null;
try {
fin = new FileInputStream(new File("E:\\Project\\Journalling\\docs\\readFile.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
java.util.Scanner scanner = new java.util.Scanner(fin, "UTF-8").useDelimiter("--AABBCCDDEEFFGGHHIIaabbccdd");
String theString = null;
while (scanner.hasNext()) {
theString = scanner.next();
System.out.println(theString);
functionToProcessStreams(theString); // This will actually do the processing.
}
scanner.close();
}
}
}
但是,我不确定这是否是最有效的方法。想到的另一件事是在输入流上使用read(b, off, len) 函数,然后处理每个字节数组。但是,为此我需要知道分隔符的索引,它可能再次读取整个流。
请建议是否有更好的方法来做到这一点。
【问题讨论】:
标签: java io inputstream outputstream