我是编程新手,仍在大学学习。我有一个任务,我很困惑。我需要使用缓冲流 I/O 搜索关键字列表并返回包含关键字的行。请帮助我,因为我不知道该怎么做。我的代码如下,它显示了
public class BufferedIO {
private String outFile = "Result.txt";
private String responseFile = "ResposeTime.txt";
private long startTime, elapsedTime; // for speed benchmarking
private int bufferSizeKB = 1;
private int bufferSize = bufferSizeKB * 2048;
private String resultHolder;
Map<String, String> keywordMap = new HashMap<String, String>();
BufferedInputStream bIn = null;
FileInputStream fIn = null;
public void ReadBufferedIOStream(String searchKeyword, String inFile) {
// Using Buffered Stream I/O
System.out.println("Reading file using Buffered Stream");
try (BufferedInputStream bIn = new BufferedInputStream(new FileInputStream(inFile));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile))) {
try( BufferedOutputStream resp =
new BufferedOutputStream(new FileOutputStream(responseFile))) {
startTime = System.nanoTime();
int bytesCount;
byte[] contents = new byte[80];
while ((bytesCount = bIn.read(contents)) != -1) {
resultHolder = new String(contents, 0, bytesCount);
if(resultHolder.contains(searchKeyword)) {
//System.out.println("\n "+resultHolder);
keywordMap.put(searchKeyword, resultHolder);
System.out.println("this is map "+keywordMap);
}
out.write(contents, 0, bytesCount);
}
elapsedTime = System.nanoTime() - startTime;
//System.out.println("The output file contains \n"+out);
System.out.println("Elapsed Time is " + (elapsedTime / 1000000.0) + " msec");
}
} catch (IOException ex) {
ex.printStackTrace();
}
finally {
// close the streams using close method
try {
if (fIn != null) {
fIn.close();
}
if (bIn != null) {
bIn.close();
}
}
catch (IOException ioe) {
System.out.println("Error while closing stream : " + ioe);
}
}
}
}