Java实现从ClearCase的.mrgman文件中提取出文件列表的程序
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
/**
* Author: Zhusheng3@126.com
* Date:2007-12-9 上午06:38:24
* Shenzhen
*/
public class MergeManFileTool
{
public static File getDoubleQuotsString(File fileEndwithmrgman
)
{
File outputFile = new File("temp/getDoubleQuotsString.txt");
outputFile.getParentFile().mkdirs();
BufferedReader br = null;
BufferedWriter bw = null;
int beginPos = 0;
int endPos = 0;
try
{
br = new BufferedReader(new FileReader(fileEndwithmrgman));
bw = new BufferedWriter(new FileWriter(outputFile));
String line = br.readLine();
boolean needRightQuot = false;
int begin = 0;
while (line != null)
{
int position = 0;
if (begin < line.length())
{
position = line.indexOf("/"", begin);
while (position >= 0)
{
if (needRightQuot == true)
{
String result = line.substring(begin - 1,
position + 1);
bw.write(result);
bw.newLine();
needRightQuot = false;
}
else
{
needRightQuot = true;
}
begin = position + 1;
position = line.indexOf("/"", begin);
}
}
if (needRightQuot == true)
{
bw.write("--------------this line need a /" to close----:");
bw.newLine();
bw.write(line);
bw.newLine();
}
line = br.readLine();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (bw != null)
{
try
{
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return outputFile;
}
public static File getLineWhichIsFile(File in)
{
File out = new File("temp//getLineWhichIsFile.txt");
BufferedReader br = null;
BufferedWriter bw = null;
int beginPos = 0;
int endPos = 0;
try
{
br = new BufferedReader(new FileReader(in));
bw = new BufferedWriter(new FileWriter(out));
String line = br.readLine();
boolean isend = false;
int begin = 0;
while (line != null)
{
// 包含文件的行,把只有文件夹的行去除掉。
if (line.contains("."))
{
bw.write(line);
bw.newLine();
}
line = br.readLine();
}
}
catch (Exception e)
{
System.out.println("--" + beginPos + ":" + endPos);
e.printStackTrace();
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (bw != null)
{
try
{
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return out;
}
public static File getLineBeginWith(File inputFile,
String prefix)
{
File resultFile = new File("temp//getAllFiles.txt");
BufferedReader br = null;
BufferedWriter bw = null;
int beginPos = 0;
int endPos = 0;
try
{
br = new BufferedReader(new FileReader(inputFile));
bw = new BufferedWriter(new FileWriter(resultFile));
String line = br.readLine();
boolean isend = false;
int begin = 0;
while (line != null)
{
if (line.startsWith(prefix))
{
bw.write(line.substring(1,line.length()-1));
bw.newLine();
//System.out.println(line.substring(1,line.length()-1));
}
line = br.readLine();
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (bw != null)
{
try
{
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return resultFile;
}
public static File getFileInTypes(File in,String []types)
{
File out = new File("temp//getAllFilesInTypes.txt");
BufferedReader br = null;
BufferedWriter bw = null;
int beginPos = 0;
int endPos = 0;
try
{
br = new BufferedReader(new FileReader(in));
bw = new BufferedWriter(new FileWriter(out));
String line = br.readLine();
boolean isend = false;
int begin = 0;
while (line != null)
{
//
File eachFile = new File(line);
if(FileTool.isFileInTypes(eachFile, types))
{
bw.write(line);
bw.newLine();
}
line = br.readLine();
}
}
catch (Exception e)
{
System.out.println("--" + beginPos + ":" + endPos);
e.printStackTrace();
}
finally
{
if (br != null)
{
try
{
br.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
if (bw != null)
{
try
{
bw.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
return out;
}
public static File parseFile_mrgman2Txt(File mrgmanFile)
{
//step1:get all string in double quots
File quots = getDoubleQuotsString(mrgmanFile);
//step2:get all lines including clearcase disk symbol-M:
File include = getLineBeginWith(quots,"/"M:");
//step3:get all lines which is file ,not a diretory
File allFiles=getLineWhichIsFile(include);
//step4:get all lines which file is in determined types
File fileInType=getFileInTypes(allFiles,FileTool.types);
return fileInType;
}
public static void main(String[] args)
{
File mrgman = new File("tw34.mrgman");
getDoubleQuotsString(mrgman);
File result =parseFile_mrgman2Txt(mrgman);
result.renameTo(new File("result.txt"));
}
}