JAVA和C# 读取文件效率问题,如何处理大文件

夏天的枫 2017-08-18 10:00:42
从C#玩到JAVA,虽然在语法上没有特别大的难点,但是发现了个问题,为啥两个的效率差那么多
最近在做一个解析工作,简单来说就是读取一个200M的txt文件,然后根据每行的数据调取接口,把返回结果存入到另外一个txt,原来的java代码时一个大神写的,我也想模仿他的多线程模式,于是乎在开始就遇到了个难题
首先,我想读取到这个txt究竟有多少行~
JAVA代码

File file = new File(filePath);
if (file.exists()) {
// allreadyRunIds
List<String> lines = null;
try {
lines = IOUtils.readLines(new FileInputStream(file), "utf-8");
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
allreadyRunIds.addAll(lines);
System.out.println("数据行数:" + lines.size());
}

C#代码

StreamReader sr = new StreamReader(path);
List<string> list = new List<string>();
int i = 0;
string readLine = sr.ReadLine();
while(readLine!=null&&readLine!="")
{
//list.Add(readLine); 用List过会儿直接崩了
i++;
sr.ReadLine();
}
Console.WriteLine(i);
Console.ReadKey();

结果JAVA结果秒出,而C#代码我已经跑了半小时了 还没见结果。。。
特此发帖,共商大计,C#如何处理这种大文件
...全文
240 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
C#效率高就行了,只要是带C的语言,效率都高,你读的慢,是你代码没写好
一品梅 2017-08-18
  • 打赏
  • 举报
回复
呵呵。。。 StreamReader sr = new StreamReader(path); List<string> list = new List<string>(); int i = 0; string readLine = sr.ReadLine(); while(readLine!=null&&readLine!="") { //list.Add(readLine); 用List过会儿直接崩了 i++; readline=sr.ReadLine(); } Console.WriteLine(i); Console.ReadKey();
夏天的枫 2017-08-18
  • 打赏
  • 举报
回复
那在处理这种事情来说,java和C#各有什么优势么
夏天的枫 2017-08-18
  • 打赏
  • 举报
回复
引用 1 楼 hanjun0612 的回复:
readLine!=null&&readLine!="" 你的代码有问题。。。你while中的readLine又不会变。。。 StreamReader sr = new StreamReader("fileName.txt"); string line; while((line= sr.ReadLine()) != null) { Console.WriteLine("xml template:"+line); } if (sr != null)sr.Close();
二了 妈的。。。
正怒月神 2017-08-18
  • 打赏
  • 举报
回复
readLine!=null&&readLine!="" 你的代码有问题。。。你while中的readLine又不会变。。。 StreamReader sr = new StreamReader("fileName.txt"); string line; while((line= sr.ReadLine()) != null) { Console.WriteLine("xml template:"+line); } if (sr != null)sr.Close();

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧