如何使用缓冲流I/O Java从文本文件中搜索关键字列表

如何使用缓冲流I/O Java从文本文件中搜索关键字列表,java,string,file,search,bufferedstream,Java,String,File,Search,Bufferedstream,我是编程新手,还在大学学习。我有一个作业,我很困惑。我需要使用缓冲流I/O搜索关键字列表,并返回包含关键字的行。请帮帮我,因为我不知道怎么做。 我的代码如下,它显示了 public class BufferedIO { private String outFile = "Result.txt"; private String responseFile = "ResposeTime.txt"; private long startTime, elapsedT

我是编程新手,还在大学学习。我有一个作业,我很困惑。我需要使用缓冲流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);
        }

    }
}
公共类BufferedIO{
私有字符串outFile=“Result.txt”;
私有字符串responseFile=“ResposeTime.txt”;
专用长启动时间,elapsedTime;//用于速度基准测试
private int bufferSizeKB=1;
private int bufferSize=bufferSizeKB*2048;
私有字符串resultHolder;
Map关键字Map=newhashmap();
BufferedInputStream bIn=null;
FileInputStream fIn=null;
public void ReadBufferedIOStream(字符串搜索关键字,字符串填充){
//使用缓冲流I/O
System.out.println(“使用缓冲流读取文件”);
try(BufferedInputStream bIn=new BufferedInputStream(new FileInputStream(infle));
BufferedOutputStream out=新的BufferedOutputStream(新文件输出流(输出文件))){
try(缓冲输出流响应)=
新的BufferedOutputStream(新的FileOutputStream(responseFile))){
startTime=System.nanoTime();
int字节计数;
字节[]内容=新字节[80];
而((字节数=bIn.read(contents))!=-1){
resultHolder=新字符串(内容,0,字节数);
if(resultHolder.contains(searchKeyword)){
//System.out.println(“\n”+resultHolder);
关键词map.put(searchKeyword,resultHolder);
System.out.println(“这是地图”+关键字地图);
}
out.write(内容,0,字节数);
}
elapsedTime=System.nanoTime()-startTime;
//System.out.println(“输出文件包含\n”+out);
System.out.println(“运行时间为”+(elapsedTime/1000000.0)+“毫秒”);
}   
}捕获(IOEX异常){
例如printStackTrace();
}
最后{
//使用close方法关闭流
试一试{
如果(fIn!=null){
fIn.close();
}
如果(bIn!=null){
bIn.close();
}
}
捕获(ioe异常ioe){
System.out.println(“关闭流时出错:+ioe”);
}
}
}

}

您是否绝对确定它只能是一条流,并且不能使用读卡器?因为
BufferedReader
具有从文本文件读取整行的
readLine()
功能。是,因为分配状态为使用缓冲流I/O。