搜索的文件中的字符串和写入与匹配字符串的行到另一个 文件需要15 - 20分钟为70MB的一个zip文件(压缩状态)。 有什么方法可以将其最小化。搜索字符串和写入匹配的行到另一个文件中的Java
我的源代码:
越来越Zip文件条目
zipFile = new ZipFile(source_file_name);
entries = zipFile.entries();
while (entries.hasMoreElements())
{ ZipEntry entry = (ZipEntry)entries.nextElement();
if (entry.isDirectory())
{
continue;
}
searchString(Thread.currentThread(),entry.getName(), new BufferedInputStream (zipFile.getInputStream(entry)), Out_File, search_string, stats); }
zipFile.close();
检索串
public void searchString(Thread CThread, String Source_File, BufferedInputStream in, File outfile, String search, String stats) throws IOException
{
int count = 0;
int countw = 0;
int countl = 0;
String s;
String[] str;
BufferedReader br2 = new BufferedReader(new InputStreamReader(in));
System.out.println(CThread.currentThread());
while ((s = br2.readLine()) != null)
{
str = s.split(search);
count = str.length - 1;
countw += count; //word count
if (s.contains(search))
{
countl++; //line count
WriteFile(CThread,s, outfile.toString(), search);
}
}
br2.close();
in.close();
}
--------------------------------------------------------------------------------
public void WriteFile(Thread CThread,String line, String out, String search) throws IOException
{
BufferedWriter bufferedWriter = null;
System.out.println("writre thread"+CThread.currentThread());
bufferedWriter = new BufferedWriter(new FileWriter(out, true));
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.flush();
}
请帮助我。对于使用线程的10个文件来说,它确实需要40分钟,在压缩后对于70MB的单个文件来说需要15-20分钟。任何方式来最小化时间。
你只是使用一个线程。你的意思是使用多个?此外,你甚至没有使用countw,countl和count vars。摆脱你的分裂呼叫。 – 2010-05-18 13:46:34
是的,我试图使用多个线程。一个用于文件夹中的每个文件。但即使是单个文件,这个过程也需要将近20分钟。所以想要最小化时间。 – Geeta 2010-05-18 13:49:47
您也可以在单个文件上使用多个线程。 FileChannel可以锁定文件的区域进行处理(请参阅http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/FileChannel.html#lock%28long,%20long,% 20boolean%29)。 – 2010-05-18 14:13:11