用输入输出流去读就好了,读取的时候按行读,然后把每一行作为一个string进行给定字符的统计就可以了

解决方案 »

  1.   

    File file = new File("c:\\test.dat");
    BufferedReader in = new BufferedReader(new InputStreamReader(
    new FileInputStream(file)));
    String line = in.readLine();
      

  2.   

    但如果“OPQ”分开两行显示呢?
      

  3.   

    但如果“OPQ”分开两行显示呢?
    这也确实是个问题呢 !
    还有就是这是个很大的文件啊 应该解决下读取性能的问题,
    也就是到底该用读哪个读取流呢!
      

  4.   

    fengmingjie(木林森)  你还是个我个完整的程序吧!
    好用就行 先不考虑性能了 ,给50分作个报酬吧 
    谢谢
      

  5.   

    如果是大文件 推荐FileChannel
    然后做统计.
      

  6.   

    package com.file;import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;public class ReadLongFile { int tmpIndex = 0; int staticCount = 0; int totalCount = 0; public void readFile(String fileName, String compareStr) {
    String tmpStr1 = "";
    String tmpStr2 = "";
    String lineStr = "";
    int count = 0;
    int compareStrLen = compareStr.length();
    try {
    File file = new File(fileName);
    BufferedReader in = new BufferedReader(new InputStreamReader(
    new FileInputStream(file)));
    lineStr = in.readLine();
    while (lineStr != null) {
    System.out.println("wwwww:" + lineStr);
    if (lineStr.indexOf(compareStr) > 0) {
    totalCount = totalCount
    + this.staticStr(lineStr, compareStr);
    staticCount = 0;
    } else if (lineStr.indexOf(compareStr) == 0) {
    totalCount++;
    }
    tmpStr1 = lineStr.substring(lineStr.length() + 1
    - compareStrLen);
    System.out.println("tmpStr1:" + tmpStr1);
    lineStr = in.readLine();
    if (lineStr != null) {
    tmpStr2 = lineStr.substring(0, compareStrLen - 1);
    System.out.println("tmpStr2:" + tmpStr2);
    if ((tmpStr1 + tmpStr2).indexOf(compareStr) >= 0) {
    totalCount++;
    }
    }
    }
    System.out.println("totalCount::" + totalCount);
    } catch (Exception e) {
    e.printStackTrace();
    }
    } public int staticStr(String sourceStr, String compareStr) {
    tmpIndex = sourceStr.indexOf(compareStr);
    if (tmpIndex < 0) {
    return staticCount;
    } else {
    tmpIndex = tmpIndex + compareStr.length();
    staticCount++;
    staticStr(sourceStr.substring(tmpIndex), compareStr);
    }
    return staticCount;
    } public static void main(String[] args) {
    ReadLongFile rf = new ReadLongFile();
    rf.readFile("c:\\test.txt", "NPQ");
    }
    }