在java中将多行拆分为一个数组

时间:2017-10-24 10:59:45

标签: java regex

我有来自sql脚本的数据,即:

-- UPGRADE KEY 1505282610114
-- USER atiwary
-- TIMESTAMP 13-Sep-2017 11:33:30 AM IST
-- DESCRIPTION 

--Desc [IPCSDD_Test1.Name] Name of the test$

--Desc [IPCSDD_Test1.Id]   ID FOR THE TEST


--Desc [IPCSDD_Test1.venue]  Venue  For the test


CREATE TABLE IPCSDD_good (Name char(20),Id int,Venue varchar(30)); 


--Desc [IPCSDD_Test1.Name] Name of the test


--Desc [IPCSDD_Test1.Name] Name of the test


--Desc [IPCSDD_Test1.Name] Name of the test


--Desc [IPCSDD_Test1.Name] Name of the test 


CREATE TABLE IPCSDD_good (Name char(20),Id int,Venue varchar(30));


--Desc [IPCSDD_Test1.Name] Name of the test$


--Desc [IPCSDD_Test1.Name] Name of the test


--Desc [IPCSDD_Test1.Name] Name of the test


CREATE TABLE IPCSDD_good (Name char(20),Id int,Venue varchar(30)); 

我需要使用分号分割字符串,这是我所做的代码无效的代码。

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    List<String> list= new  ArrayList<String>();
    List<String>listClone =new ArrayList<String>();
     List<String>listClone2 =new ArrayList<String>(); 

    Pattern regex2 = Pattern.compile(".*\\bCREATE\\b.*");
    try{
        String srcDir="D:\\Users\\jvarughese\\Documents\\SQL Server Management Studio\\upgradedir";


        File folder=new File(srcDir);

        File[] listOfFiles=folder.listFiles();

            for(int i=0;i<listOfFiles.length;i++){


                if(listOfFiles[i].isFile()){


                    BufferedReader in =new BufferedReader(new FileReader(listOfFiles[i].getPath()));
                    String str;
                while((str=in.readLine())!=null)    {
                    if(str.length()>0){
                        str=str.trim();
                        list.add(str);
                        }

                    Matcher m2=regex2.matcher(str);
                    if(m2.find()){
                        listClone.addAll(list);
                    }


                    }

                for(String docString :listClone){
                    String newDocString=docString.replaceAll("(\\\\r|\\\\n|\\\\\r\\\\\n)+","\\\\\n") ; 
                    System.out.println(newDocString);
                    for(String names : newDocString.split(";", -1)){ 
                        //System.out.println(names);
                        //System.out.println("==================");

                    }

                    }

                    }
    }

2 个答案:

答案 0 :(得分:0)

请使用 list.add(str)而不是 listClone.addAll(list);

             BufferedReader in =new BufferedReader(new 
             FileReader(listOfFiles[i].getPath()));
                String str;
            while((str=in.readLine())!=null)    {
                if(str.length()>0){
                    str=str.trim();
                    list.add(str);
                    }

                Matcher m2=regex2.matcher(str);
                if(m2.find()){
                    listClone.add(str); 
                }
      ...................

答案 1 :(得分:0)

也许某些行没有真正的换行符?

相关问题