如何在java中对文件的内容进行排序?

问题描述 投票:0回答:1

我正在构建 Java EE 服务器,但遇到了问题。在应用程序中,有两个连接分别针对不同的数据库服务器。一种用于 A_TableName,另一种用于 B_TableName。在应用程序中,我使用了一种名为 QueryReader 的方法。它接受参数:文件名和文件路径,并返回 Sql 查询的 arrayList。我想根据它们是从 A 表还是 B 表调用来对它们进行排序:

SQL 查询的文件内容: `#查询号码 选择 A_COL、B_COL、C_COL 来自 A_TABLENAME

#查询号码 选择 A_COL、B_COL、C_COL 来自 B_TABLENAME`

QUERYREADer 文件: `public ArrayList createQueries(String path) 抛出 IOException

    {   
        String queryLine =      new String();  
        StringBuffer sBuffer =  new StringBuffer();  
        listOfQueries =         new ArrayList<String>();  
          
        try    
        {    
            FileReader fr =     new FileReader(new File(path));         
            BufferedReader br = new BufferedReader(fr);    
        
            //read the SQL file line by line  
            while((queryLine = br.readLine()) != null)    
            {    
                // IGNORE COMMENTS BEGINNING WITH #  
                int indexOfCommentSign = queryLine.indexOf('#');  
                if(indexOfCommentSign != -1)  
                {  
                    if(queryLine.startsWith("#"))  
                    {  
                        queryLine = new String("");  
                    }  
                    else   
                        queryLine = new String(queryLine.substring(0, indexOfCommentSign-1));  
                }  
                // IGNORE COMMENTS BEGINNING WITH --  
                indexOfCommentSign = queryLine.indexOf("--");  
                if(indexOfCommentSign != -1)  
                {  
                    if(queryLine.startsWith("--"))  
                    {  
                        queryLine = new String("");  
                    }  
                    else   
                        queryLine = new String(queryLine.substring(0, indexOfCommentSign-1));  
                }  
                // IGNORE COMMENTS SURROUNDED BY /* */  
                indexOfCommentSign = queryLine.indexOf("/*");  
                if(indexOfCommentSign != -1)  
                {  
                    if(queryLine.startsWith("#"))  
                    {  
                        queryLine = new String("");  
                    }  
                    else  
                        queryLine = new String(queryLine.substring(0, indexOfCommentSign-1));  
                      
                    sBuffer.append(queryLine + " ");   
                    // IGNORE ALL CHARACTERS WITHIN THE COMMENT  
                    do  
                    {  
                        queryLine = br.readLine();  
                    }  
                    while(queryLine != null && !queryLine.contains("*/"));  
                    indexOfCommentSign = queryLine.indexOf("*/");  
                    if(indexOfCommentSign != -1)  
                    {  
                        if(queryLine.endsWith("*/"))  
                        {  
                            queryLine = new String("");  
                        }  
                        else  
                            queryLine = new String(queryLine.substring(indexOfCommentSign+2, queryLine.length()-1));  
                    }  
                }  
                  
                //  THE + " " IS NECESSARY, BECAUSE OTHERWISE THE CONTENT BEFORE AND AFTER A LINE BREAK ARE CONCATENATED  
                //  LIKE E.G. A.XYZ FROM BECOMES A.XYZFROM OTHERWISE AND CAN NOT BE EXECUTED   
                if(queryLine != null)  
                    sBuffer.append(queryLine + " ");    
            }    `

尝试对包含 sql 查询的文件内容进行排序。无论他们使用的是一张桌子还是另一张桌子。

java file filereader
1个回答
0
投票

假设我理解你的问题,我会使用 SQL

UNION ALL
来组合两个查询 添加一个合成列来指示源表。喜欢,

SELECT 'A' as TBL_NAME, A_COL, B_COL, C_COL FROM A_TABLENAME
UNION ALL
SELECT 'B' as TBL_NAME, A_COL, B_COL, C_COL FROM B_TABLENAME
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.