急啊 文件读取的问题 用输入输出流去读就好了,读取的时候按行读,然后把每一行作为一个string进行给定字符的统计就可以了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 File file = new File("c:\\test.dat");BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));String line = in.readLine(); 但如果“OPQ”分开两行显示呢? 但如果“OPQ”分开两行显示呢?这也确实是个问题呢 !还有就是这是个很大的文件啊 应该解决下读取性能的问题,也就是到底该用读哪个读取流呢! fengmingjie(木林森) 你还是个我个完整的程序吧!好用就行 先不考虑性能了 ,给50分作个报酬吧 谢谢 如果是大文件 推荐FileChannel然后做统计. 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"); }} JAVA环境配置谁会不示案 请教学习方向 UT Case 是什么啊 初学spring的简单问题!请大家帮忙 用SSH开发的两个卖场网站 Myeclipse remove已添加项目时报错 struts2 新手求助 在线等 想要一个servlet+jsp的源码:如何将ejb中返回的collection类型先显示了前10个,然后按下一页,继续显示剩下的10个...... 【求助和讨论】J2EE可以用来做电信计费吗? 删除 console configuration文件 JNI性能问题求教 鼠标移过去可以显示全文如何实现???请高手指点
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(file)));
String line = in.readLine();
这也确实是个问题呢 !
还有就是这是个很大的文件啊 应该解决下读取性能的问题,
也就是到底该用读哪个读取流呢!
好用就行 先不考虑性能了 ,给50分作个报酬吧
谢谢
然后做统计.
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");
}
}