阅读大容量txt文件并写入txt文件中

chenai520123 2014-07-15 04:16:57
从txt文件中读取文件(txt有200w行),并写入txt中。有没有高手给个多线程读写txt的demo。我写的代码如下,但时间太长

求高手指教
package SavereadTxt;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class readtxt2
{
private static final String FULL_LOCALPATH = null;
private static final String LOCAL_PATH = null;


/* private String[] parserData(String data)
{
// 查询已经使用的实时标签配置参数ID
List list = AttributeFileLabelService.getInstance().queryRule();
// 以"|"分隔
String[] realTimeInfoArray = data.split("\\|", -1);
String[] writer = new String[0];
int phoneValue = AttributeFileLabelService.getInstance().queryPhoneValue();
if (list != null && !list.isEmpty() && realTimeInfoArray.length > 1)
{
writer = new String[list.size() * realTimeInfoArray.length];
int columnValue = -1;
String realTimeValue = "";
String value = "";
String labelId = "";
boolean isTrue = false;
int k = 0;
String telephone = "";
for (int i = 0; i < list.size(); i++)
{
Hashtable hm = (Hashtable)list.get(i);
realTimeValue = ADMUtil.tostring(hm.get("realtime_value"), "");
columnValue = Integer.parseInt(ADMUtil.tostring(hm.get("column_value"), ""));
labelId = ADMUtil.tostring(hm.get("label_id"), "");
if (columnValue >= 1 && columnValue <= realTimeInfoArray.length && phoneValue > 0)
{
value = realTimeInfoArray[columnValue - 1];
telephone = realTimeInfoArray[phoneValue - 1];
if (ADMUtil.tostring(hm.get("realtime_match_mode"), "").equals("0"))
{
isTrue = parseEqual(value, realTimeValue);// 相等
}
else if (ADMUtil.tostring(hm.get("realtime_match_mode"), "").equals("1"))
{
isTrue = parseContain(value, realTimeValue);// 包含于
}
else
{
isTrue = parseSame(value, realTimeValue);// 类似于
}
if (isTrue)
{
writer[k++] = labelId + "|" + telephone;
}
}
}

}
return writer;

}

@SuppressWarnings("rawtypes")*/

public void analyseAttributeFile()
{
// 本地存放话单文件的目录
File localDir = new File(FULL_LOCALPATH);
File[] fileList = localDir.listFiles();
String[] strs = null;
// 如果本地经分文件话单目录不存在或则为空,直接return
if (!localDir.exists() || fileList.length == 0)
{
System.out.println("Local CDR directory dose not exists or is empty! localPath:" + FULL_LOCALPATH);
return;
}
try
{
File localFile = new File(LOCAL_PATH);
if (!localFile.exists())
{
if (!localFile.mkdir())
{
System.out.println("mkdir file error!LOCAL_PATH:" + LOCAL_PATH);
return;
}
}
String str = "";
for (File file : fileList)
{
// 过滤目录
if (file.isDirectory())
{
continue;
}
String id;
StringBuffer phone;
BufferedReader br = null;
BufferedWriter wr = null;
Map<String, StringBuffer> map = new HashMap<String, StringBuffer>();
br = new BufferedReader(new FileReader(file));
StringBuffer[] sb;
while ((str = br.readLine()) != null)
{

if (str != null)
{
id = str.split("\\|")[0];
phone = new StringBuffer(str.split("\\|")[1]);
if (map.containsKey(id))
{
map.put(id, phone.append("\r" + map.get(id)));
}
else
{
map.put(id, phone);
}
}

}
// 关闭流
for (Entry<String, StringBuffer> entry : map.entrySet())
{

wr = new BufferedWriter(new FileWriter(new File(LOCAL_PATH + "/" + entry.getKey() + ".TXT"), true));
wr.write(entry.getValue().toString()+ "\r");
wr.close();
}
br.close();
}

}
catch (Exception e)
{

System.out.println("analyseAttribute File failed!");
e.printStackTrace();
}
}

}
...全文
1093 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
attilax 2014-08-05
  • 打赏
  • 举报
回复
5.你的这个代码有好几个性能问题.. 1.提升文件io性能 要把BufferedWriter 的建立and 流的关闭放在循环外面哟..这样添加200w数据只需要7秒....否则大概要2小时了。。。 BufferedWriter wr= new BufferedWriter(new FileWriter(new File("c:\\200w.txt"),true)); // 关闭流 for (Entry<String, StringBuffer> entry : map.entrySet()) { StringBuffer sb=entry.getValue(); wr.write( sb.toString()+ "\r\n"); } wr.close(); 2.提升map性能,设置初始化值 new HashMap<String, StringBuffer>(2000000); 在我的机器上,添加200w大概需要9秒..要是不设置初始化值,大概要15秒左右.. 此外,200w 的map大概要使用内存500M 左右了.. 3.循环读取文件的问题 while ((str = br.readLine()) str.split("\\|")[1] 这样str.split 是使用正则表达式的,也是性能慢的...
xuzuning 2014-07-17
  • 打赏
  • 举报
回复
1、你发错地方了!CSDN 辟有专门的 Java 讨论区,Java 高手都在那边 2、如果你的文件需要反复使用的话,那么应先构造一个索引文件,这样就可快速移动文件指针了 你期望的多线程就更少不了这个索引了 3、如果你的文件只是单次使用,那么应导入到数据库(数据库都提供有导入文本文件的功能,秒杀)然后再处理
chenai520123 2014-07-17
  • 打赏
  • 举报
回复
这么大的数据就是往数据库里写也要很长时间,我需要缩短这段时间
巴山虎 2014-07-16
  • 打赏
  • 举报
回复
我猜应该是和网络小说相关的。
巴山虎 2014-07-16
  • 打赏
  • 举报
回复
不知道有什么大用处。 txt写到txt? 直接上传txt文件不更好?
码无边 2014-07-16
  • 打赏
  • 举报
回复
就算文件写入txt,你可以打开文件? 可以将txt写入内存或者数据库。
a13849178851 2014-07-16
  • 打赏
  • 举报
回复
将txt写入内存或者数据库
chenai520123 2014-07-16
  • 打赏
  • 举报
回复
要读的txt文件是有tag的eg:11 |1111111111111 22|22222222222 33|33333333333 根据tag 将tag的值获取并分类
韩誉 2014-07-15
  • 打赏
  • 举报
回复
这么大不要全部读入吧,你可以记录当前读取位置和显示,把前一段显示和后一段显示读到内存中,切换显示了再前后读取缓存不就快了

87,925

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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