Java - 文件比较;在下一个并行行中搜索字符串(忽略空格) [英] Java - File compare; Search string in next concurrent line(Ignore whitespace)

查看:133
本文介绍了Java - 文件比较;在下一个并行行中搜索字符串(忽略空格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中尝试文件比较。
如何在下一个行中搜索字符串(忽略空格)



例如:

  ** File1 ** ** File2 ** 
< name> abc< / name> | < name> abc< / name>等于
< age> 21< / age> |不等于
< company> zyz< / company> | < age> 21< / age>不等于
| < company> zyz< / company>不等于


目前使用我的逻辑, CSV


 < name> abc< / name>,< age> 21< / age ;< company> zyz< / company>,####目标文件开始####,
< name> abc< / name> ;,,< age> 21< / age& < company> zyz< / company>


问题:
在CSV的第二行中,年龄值为空。当我的程序检查File1中的第二行和File2中的第二行时,它们不相等,因此它也给相应的行为false。



要实现:我必须忽略空格,并检查下一个出现的值,如果两者相等,则向下移动file1的第二行。



必须像这样


  ** File1 ** ** File2 ** 
< ; name> abc< / name> | < name> abc< / name>
|
< age> 21< / age> | < age> 21< / age>
< company> zyz< / company> | < company> zyz< / company>


这里是我到目前为止试过

  public List< String> FileCompare(String source,String target){
try {
// String source =D:/reference.xml;
// String target =D:/comparison.xml;
// Diff d = new Diff(myControlXML,myTestXML);
FileReader fr = new FileReader(source);
FileReader fr1 = new FileReader(target);

BufferedReader br = new BufferedReader(fr);
BufferedReader br2 = new BufferedReader(fr1);

String s1,s2;

String st = new String();
String st2 = new String();


while((s1 = br.readLine())!= null){
myList.add(s1);
st = st.concat(s1);
//System.out.println(s1);
}

Collections.addAll(myList,######### Start of target #########);

while((s2 = br2.readLine())!= null){

st2 = st2.concat(s2);

myList1.add(s2);
}
myList.addAll(myList1);
System.out.println(myList);
//System.out.println(myList1);
}
catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}
return myList;
}

以下是主要代码

 比较c = new Compare(); 
// FileCompare返回CSV字符串
List< String> s = c.FileCompare(D:/reference.xml,D:/comparison.xml);

String pdf = s.toString();
String [] tokens = pdf.split(,);
for(String token:tokens)
System.out.println(token);

如何通过忽略空格来做这件事?请帮助



很多谢谢!

解决方案

一个字符串,如果它不为null,不检查它是否实际上充满了空白。尝试以下操作以防止附加空行:

  s1 = br.readLine 
while(s1!= null){
if(s1.trim()。length()> 0){
myList.add(s1);
st = st.concat(s1);
//System.out.println(s1);
}
s1 = br.readLine();
}

同样适用于 s2 和他的循环。


Im trying File comparison in Java. How do I search for string in the next occuring line(ignoring whitespace)

Eg:

       **File1**                **File2**
     <name>abc</name>  |   <name>abc</name>              Equal
        <age>21</age>  |                                 Not Equal
<company>zyz</company> |   <age>21</age>                 Not Equal
                       |   <company>zyz</company>        Not Equal

Currently with my logic, I've a string with CSV

<name>abc</name>, <age>21</age>, <company>zyz</company>, ####Start of target File####,
<name>abc</name>,              , <age>21</age>, <company>zyz</company>

Problem: In the second line of CSV the age value is blank. When my program checks the second line in File1 and second line in File2 they are not equal hence it gives false for the corresponding lines as well.

What needs to be achieved: I must ignore white space and check for the next occuring value, if both are equal then move the second line of file1 down.

The output must look like this

       **File1**                **File2**
     <name>abc</name>  |   <name>abc</name>
                       |
        <age>21</age>  |   <age>21</age>
<company>zyz</company> |   <company>zyz</company>

here is what I've tried so far

public List<String> FileCompare(String source, String target) {
try {
    //String source="D:/reference.xml";
    //String target="D:/comparison.xml";
    //Diff d=new Diff(myControlXML, myTestXML);
    FileReader fr = new FileReader(source); 
    FileReader fr1 = new FileReader(target);

    BufferedReader br = new BufferedReader(fr);
    BufferedReader br2 = new BufferedReader(fr1);

    String s1,s2;

    String st= new String();
    String st2= new String();


    while((s1 = br.readLine()) != null) { 
            myList.add(s1);
            st=st.concat(s1);   
            //System.out.println(s1);
    } 

    Collections.addAll(myList, "#########Start of target#########");

    while((s2 = br2.readLine())!=null){

        st2=st2.concat(s2);

        myList1.add(s2);
    }
    myList.addAll(myList1);
    System.out.println(myList);
    //System.out.println(myList1);
}
catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return myList;
}

Here is the main Code

    Compare c=new Compare();
//FileCompare returns CSV string
List<String> s=c.FileCompare("D:/reference.xml", "D:/comparison.xml"); 

String pdf=s.toString();
String[] tokens=pdf.split(",");
for(String token:tokens)
    System.out.println(token);

How do I go about doing this by ignoring whitespace? Please help

many Thanks!

解决方案

Both of your loops concat a String if it's not null, without checking if it's actually full of whitespaces. Try the following to prevent appending blank lines:

s1 = br.readLine();
while(s1 != null) { 
    if(s1.trim().length() > 0) {
        myList.add(s1);
        st=st.concat(s1);   
        //System.out.println(s1);
    }
    s1 = br.readLine();
}

The same goes for s2 and his loop.

这篇关于Java - 文件比较;在下一个并行行中搜索字符串(忽略空格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆