如何使用Java查找字符串的循环依赖项

如何使用Java查找字符串的循环依赖项,java,Java,如果我有两个文件,比如ABCD.txt和DEF.txt。我需要检查DEF.txt中是否存在字符串“ABCD”,以及DEF.txt中是否存在字符串“DEF”,并将组合写入文件 总共我有大约15000个文件,每个文件包含近50-3000行必须搜索。我写了一段代码,它很有效。。但是显示整个列表需要一个小时 有没有更好的方法来实现这一点?请推荐我 public void findCyclicDependency(){ Hashtable<String, String> htF

如果我有两个文件,比如ABCD.txt和DEF.txt。我需要检查DEF.txt中是否存在字符串“ABCD”,以及DEF.txt中是否存在字符串“DEF”,并将组合写入文件

总共我有大约15000个文件,每个文件包含近50-3000行必须搜索。我写了一段代码,它很有效。。但是显示整个列表需要一个小时

有没有更好的方法来实现这一点?请推荐我

   public void findCyclicDependency(){

    Hashtable<String, String> htFileNameList_1  = new Hashtable<String, String>();  
    Hashtable<String, String> htCyclicNameList  = new Hashtable<String, String>(); 

    FileWriter fwCyclicDepen = null;
    PrintWriter outFile = null;

    FileInputStream fstream = null;     
    FileInputStream fstream_1 = null; 

    DataInputStream in = null;
    BufferedReader br = null;

    DataInputStream in_1 = null;
    BufferedReader br_1 = null;

    String strSV_File_CK="";  

    boolean bFound = false;
    File fileToSearch = null;

    String strSVFileNameForComparison = "";
    String strSVDependencyFileLine = "";
    String strSVDFileLineExisting = "";
    String strCyclicDependencyOut = "";

    try {            
        File baseInputDirectory = new File(strInputPath);            

        List<File> baseInputDirListing = FileListing.getFileListing(baseInputDirectory);

        // Printing out the filenames for the SodaSystem
        for (File swPackage : baseInputDirListing) 
        {

            if (swPackage.isDirectory() && swPackage.getName().endsWith("Plus")) {
                List<File> currSwPackageFileListing = FileListing.getFileListing(swPackage);
                System.out.println("\n swPackage File --> " + swPackage.getName() );
                strCyclicDependencyOut = strOutputPath + "_"+ swPackage.getName() + "_CyclicDependency.xml";
        System.out.println("\n strCyclicDependencyOut File --> " + strCyclicDependencyOut );
        fwCyclicDepen = new FileWriter(strCyclicDependencyOut);
        outFile = new PrintWriter(new BufferedWriter(fwCyclicDepen));
        outFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        outFile.write("<CyclicDependencyFile>");

                 for (File DependentFile : currSwPackageFileListing) {                        
                    strSV_File_CK = DependentFile.getName().substring(0, (DependentFile.getName().length() - 4)).trim();

                    htFileNameList_1.put(strSV_File_CK.toUpperCase(),strSV_File_CK.toUpperCase());
                 }


                for (File DependentFile : currSwPackageFileListing) 
                {                        
                    fstream = new FileInputStream(DependentFile);
                    // Get the object of DataInputStream
                    in = new DataInputStream(fstream);
                    br = new BufferedReader(new InputStreamReader(in));

                    strSVFileNameForComparison = DependentFile.getName().substring(0, (DependentFile.getName().length() - 4)).trim();

                    //Read File Line By Line
                    while ((strSVDependencyFileLine = br.readLine()) != null) 
                    {
                        bFound = false;                                            

                        if (strSVDependencyFileLine.toUpperCase().indexOf("INDICES") == -1) 
                        {
                            //Check the current line matches any of the file name in software package folder
                            if (htFileNameList_1.contains(strSVDependencyFileLine.trim().toUpperCase())
                             && strSVDependencyFileLine.compareTo(strSVFileNameForComparison) != 0)
                            {  
                              bFound = true;

                              // Get the file to search
                              for (File searchFile : currSwPackageFileListing) 
                              {

                                  if((searchFile.getName().substring(0, (searchFile.getName().length() - 4)).trim()).equals(strSVDependencyFileLine))
                                  {
                                      fileToSearch = searchFile;
                                      break;
                                  }
                              }

                              // Read the file where the file name is found
                              fstream_1 = new FileInputStream(fileToSearch);

                              in_1 = new DataInputStream(fstream_1);
                              br_1 = new BufferedReader(new InputStreamReader(in_1));

                              while ((strSVDFileLineExisting = br_1.readLine()) != null)
                              {
                                  if (strSVDFileLineExisting.toUpperCase().indexOf("EXTRA") == -1) 
                                    {
                                        if (htFileNameList_1.contains(strSVDFileLineExisting.trim().toUpperCase()) && bFound 
                                                && strSVDFileLineExisting.compareTo(strSVDependencyFileLine) != 0 
                                                && strSVDFileLineExisting.compareTo(strSVFileNameForComparison) == 0 )
                                        {

                                            if(!htCyclicNameList.containsKey(strSVDependencyFileLine) && 
                                                    !htCyclicNameList.containsValue(strSVDFileLineExisting))
                                              {
                                                htCyclicNameList.put(strSVDFileLineExisting,strSVDependencyFileLine);

                                                outFile.write("<CyclicDepedency FileName = \"" +  strSVDFileLineExisting + "\""+ " CyclicFileName = \"" + 
                                                    strSVDependencyFileLine + "\" />");
                                                    break;
                                              }

                                        }
                                    }

                              }

                            }         

                        }
                        else
                        {
                            bFound = false;
                        }                               

                        }//if current line <> 

                    }// reach each line in the current file

                outFile.write("</CyclicDependencyFile>");
            } 
            outFile.flush();
            outFile.close();

        }         

    }
    catch(Exception e){
        e.printStackTrace();
    }
}
public void findCyclicDependency(){
Hashtable htFileNameList_1=新Hashtable();
Hashtable htCyclicNameList=新Hashtable();
FileWriter fwCyclicDepen=null;
PrintWriter输出文件=null;
FileInputStream fstream=null;
FileInputStream fstream_1=null;
DataInputStream in=null;
BufferedReader br=null;
_1中的DataInputStream=null;
BufferedReader br_1=null;
字符串strSV_File_CK=“”;
布尔bFound=false;
文件fileToSearch=null;
字符串strSVFileNameForComparison=“”;
字符串strSVDependencyFileLine=“”;
字符串strSVDFileLineExisting=“”;
字符串strCyclicDependencyOut=“”;
试试{
File baseInputDirectory=新文件(strInputPath);
List baseInputDirListing=FileListing.getFileListing(baseInputDirectory);
//打印出系统的文件名
for(文件swPackage:baseInputDirListing)
{
if(swPackage.isDirectory()&&swPackage.getName().endsWith(“Plus”)){
List currSwPackageFileListing=FileListing.getFileListing(swPackage);
System.out.println(“\n swPackage File-->”+swPackage.getName());
strCyclicDependencyOut=strOutputPath+““+swPackage.getName()+“CyclicDependency.xml”;
System.out.println(“\n strCyclicDependencyOut文件-->”+strCyclicDependencyOut);
fwCyclicDepen=新文件写入程序(strCyclicDependencyOut);
outFile=新的打印写入程序(新的缓冲写入程序(FWCycleCDepen));
输出文件。写(“”);
输出文件。写(“”);
对于(文件依赖文件:currSwPackageFileListing){
strSV_File_CK=DependentFile.getName().substring(0,(DependentFile.getName().length()-4)).trim();
htFileNameList_1.put(strSV_File_CK.toUpperCase(),strSV_File_CK.toUpperCase());
}
对于(文件依赖文件:currSwPackageFileListing)
{                        
fstream=新文件输入流(DependentFile);
//获取DataInputStream的对象
in=新数据输入流(fstream);
br=新的BufferedReader(新的InputStreamReader(in));
strSVFileNameForComparison=DependentFile.getName().substring(0,(DependentFile.getName().length()-4)).trim();
//逐行读取文件
而((strSVDependencyFileLine=br.readLine())!=null)
{
bFound=假;
if(strSVDependencyFileLine.toUpperCase().indexOf(“索引”)=-1)
{
//检查当前行是否与软件包文件夹中的任何文件名匹配
if(htFileNameList_1.contains)(strSVDependencyFileLine.trim().toUpperCase())
&&strSVDependencyFileLine.compareTo(用于比较的strsvFileName)!=0)
{  
bFound=真;
//获取要搜索的文件
用于(文件搜索文件:currSwPackageFileListing)
{
if((searchFile.getName().substring(0,(searchFile.getName().length()-4)).trim().equals(strSVDependencyFileLine))
{
fileToSearch=searchFile;
打破
}
}
//读取找到文件名的文件
fstream_1=新文件输入流(fileToSearch);
in_1=新数据输入流(fstream_1);
br_1=新的BufferedReader(新的InputStreamReader(in_1));
而((strSVDFileLineExisting=br_1.readLine())!=null)
{
if(strSVDFileLineExisting.toUpperCase().indexOf(“额外”)==-1)
{
if(htFileNameList_1.contains(strSVDFileLineExisting.trim().toUpperCase())和&bFound
&&strSVDFileLineExisting.compareTo(strSVDependencyFileLine)!=0
&&strsvfilelineexisting.compareTo(strsvfilenameforcomparation)=0)
{
如果(!htCyclicNameList.containsKey(strSVDependencyFileLine)&&
!htCyclicNameList.containsValue(strSVDFileLineExisting))
{
htCyclicNameList.put(strSVDFileLineExisting,strSVDependencyFileLine);
输出文件。写(“”);
打破
}
}
}
}
}         
}
其他的
{
static public void findCyclicDependency2() {
    PrintWriter outFile = null;

    Map<String,File> fileNames = new HashMap<String,File>();
    Map<String,Set<String>> fileBackward = new HashMap<String,Set<String>>();
    Map<String,Set<String>> fileForward = new HashMap<String,Set<String>>();

    try {
        File baseInputDirectory = new File(strInputPath);

        List<File> baseInputDirListing = getFileListing(baseInputDirectory);

        // Printing out the filenames for the SodaSystem
        for(File swPackage:baseInputDirListing) {

            if (! (swPackage.isDirectory()
                    || swPackage.getName().endsWith("Plus"))) continue;

            System.out.println("Loading file names");
            List<File> currSwPackageFileListing = getFileListing(swPackage);
            for(File dependentFile:currSwPackageFileListing) {
                String name = trimName(dependentFile);
                fileNames.put(name,dependentFile);

                BufferedReader br = new BufferedReader(new FileReader(dependentFile));
                String line;
                Set<String> contFor = new HashSet<String>();
                Set<String> contBack = new HashSet<String>();
                while( (line=br.readLine()) != null ) {
                    line = line.toUpperCase().trim();
                    if( line.equals("EXTRA") ) continue;
                    if( line.equals("INDICES") ) continue;
                    if( line.equals(name) ) continue;

                    if( line.compareTo(name) == 1 ) {
                        contFor.add(line);
                    } else {
                        contBack.add(line);
                    }
                }
                fileBackward.put(name,contBack);
                fileForward.put(name,contFor);
            }

            String strCyclicDependencyOut = strOutputPath + "_"
                    + swPackage.getName() + "_CyclicDependency.xml";
            outFile = new PrintWriter(new BufferedWriter(new FileWriter(strCyclicDependencyOut)));
            outFile.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            outFile.write("<CyclicDependencyFile>");

            for(Entry<String,Set<String>> entry : fileForward.entrySet()) {
                String curr = entry.getKey();
                for(String other : entry.getValue()) {
                    Set<String> otherRefs = fileBackward.get(other);
                    if( otherRefs == null ) continue;
                    if( otherRefs.contains(curr) ) {
                        outFile.write("<CyclicDepedency FileName = \""
                                + fileNames.get(curr).getPath()
                                + "\""
                                + " CyclicFileName = \""
                                + fileNames.get(other).getPath()
                                + "\" />");
                    }
                }
            }


            outFile.write("</CyclicDependencyFile>");
            outFile.flush();
            outFile.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}