Hi Friend,
1)It will print.
2)ReadFile.java:
import java.util.*;
import java.io.*;
import java.text.*;
import java.util.regex.*;
public class ReadFile{
public static void main(String args[]) throws IOException{
try {
BufferedReader reader = new BufferedReader(new FileReader("
c://a.txt";));
Pattern pat = Pattern.compile(",(\".+?\")");
String line=null;
while ((line=reader.readLine())!=null){
Matcher matcher = pat.matcher(line);
StringBuffer sb = new StringBuffer();
while (matcher.find()){
matcher.appendReplacement(sb,","+matcher.group(1).replaceAll(",","&comm"));
}
matcher.appendTail(sb);
String[] fields = sb.toString().split(",");
for (int i=0;i<fields.length;i++){
String out = fields[i]!=null ? fields[i].replaceAll("&comm",",") : "";
FileWriter fstream = new FileWriter("out.txt",true);
BufferedWriter output = new BufferedWriter(fstream);
output.write(out+",");
output.newLine();
output.close();
}
}
}catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
}
3)CompareArrays.java:
import java.util.*;
public class CompareArrays{
public static void main(String[]args){
String[] arr1={"a","fdf","h","d","e"};
String[] arr2={"a","b","ch"};
List<String> a1 = Arrays.asList(arr1);
List<String> a2 = Arrays.asList(arr2);
int array1Size = a1.size();
int array2Size = a2.size();
Set s = new HashSet();
s.addAll(a1);
s.addAll(a2);
ArrayList list = new ArrayList();
for(int i=0; i<a2.size(); i++) {
if(!a1.contains(a2.get(i))) {
list.add(a2.get(i));
}
}
for(int i=0; i<a1.size(); i++) {
if(!a2.contains(a1.get(i))) {
list.add(a1.get(i));
}
}
System.out.println("Elements which are different: ");
System.out.println("");
for(int i = 0; i < list.size(); i++){
System.out.print((String)list.get(i)+" ");
}
}
}
Thanks