简体   繁体   English

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

[英]Split multiple lines into an array in java

I have data from a sql script ie :我有来自 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)); 

I need to split string using semicolons, here's the code I have done which is not working properly.我需要使用分号拆分字符串,这是我所做的无法正常工作的代码。

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("==================");

                    }

                    }

                    }
    }

please use list.add(str) instead of listClone.addAll(list);请使用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); 
                }
      ...................

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM