sravnenie massivov
Dobrogo vremia sutok,
Zadacha sravnit dva txt i zapisat v tretij txt raznye strochki package Main; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class doublefail { public static void main(String[] args) throws IOException { List<String> lt= new ArrayList<String>();; File file = new File ("C:/Users/Jevgenij/Desktop/o.txt"); FileInputStream fis = null; //pervyj fail try{ fis= new FileInputStream(file); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String content; while ((content = br.readLine()) != null) { lt.add(content);} } catch (IOException e) { e.printStackTrace(); } finally { fis.close(); } // vtoroj fail List<String> gt= new ArrayList<String>();; File file3 = new File ("C:/Users/Jevgenij/Desktop/k.txt"); FileInputStream fis3 = null; try{ fis3= new FileInputStream(file3); BufferedReader bt = new BufferedReader(new InputStreamReader(fis3)); String content; while ((content = bt.readLine()) != null) { gt.add(content);} } catch (IOException e) { e.printStackTrace(); } finally { fis3.close(); } // output stream int t=0; OutputStream file2 = new FileOutputStream( "C:/Users/Jevgenij/Desktop/l.txt" ); OutputStream of = new BufferedOutputStream(file2); ObjectOutputStream oss= new ObjectOutputStream (of); try{ Iterator it=lt.iterator(); while(it.hasNext()){ String value=(String)it.next(); Iterator kt=gt.iterator(); while(kt.hasNext()){ //r++; String o=(String)kt.next(); if (value.equals(o)) { t++; } if(t==0){ System.out.println(value); oss.writeBytes(value); oss.write(System.getProperty("line.separator").getBytes()); t=0;} } } } finally { oss.close(); } } } |
В Chrome не работает.
|
Цитата:
Цитата:
|
Ко для IE
import java.io.*; import java.util.*; public class myfilereader { public static void main (String[] args) throws java.io.IOException { //Getting the name of the files to be compared. BufferedReader br2 = new BufferedReader (new InputStreamReader(System.in)); System.out.println("Enter 1st File name:"); String str = br2.readLine(); System.out.println("Enter 2nd File name:"); String str1 = br2.readLine(); String s1=""; String s2="",s3="",s4=""; String y="",z=""; //Reading the contents of the files BufferedReader br = new BufferedReader (new FileReader (str)); BufferedReader br1 = new BufferedReader (new FileReader (str1)); while((z=br1.readLine())!=null) s3+=z; while((y=br.readLine())!=null) s1+=y; System.out.println (); //String tokenizing int numTokens = 0; StringTokenizer st = new StringTokenizer (s1); String[] a = new String[10000]; for(int l=0;l<10000;l++) {a[l]="";} int i=0; while (st.hasMoreTokens()) { s2 = st.nextToken(); a[i]=s2; i++; numTokens++; } int numTokens1 = 0; StringTokenizer st1 = new StringTokenizer (s3); String[] b = new String[10000]; for(int k=0;k<10000;k++) {b[k]="";} int j=0; while (st1.hasMoreTokens()) { s4 = st1.nextToken(); b[j]=s4; j++; numTokens1++; } //comparing the contents of the files and printing the differences, if any. int x=0; for(int m=0;m<a.length;m++) { if(a[m].equals(b[m])){} else { x++; System.out.println(a[m] + " -- " +b[m]); System.out.println();} } System.out.println("No. of differences : " + x); if(x>0){System.out.println("Files are not equal");} else{System.out.println("Files are equal. No difference found");} } } У меня работает. |
Часовой пояс GMT +3, время: 12:54. |