Comparing text files


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Comparing text files

  1. #1
    Join Date
    Oct 2005
    Posts
    16

    Comparing text files

    Hi,
    I have a somewhat abstract question, in that i dont have any code examples for what i am trying to do. Basically i want to write some code that will take a number of files, and compare them, picking out words that appear frequently in all the files. Now im relatively new to java and still trying to get my head around it, so what im looking for is a few pointers how to get started on this. Does anybody have any suggestions?

    Thanks

  2. #2
    Join Date
    Oct 2005
    Posts
    4
    hi,
    You can take a look at this Thread.

    http://forum.java.sun.com/thread.jspa?threadID=673881

    Sincerely,

    Tien-Chih Wang

  3. #3
    Join Date
    Oct 2005
    Posts
    16
    Thanks that seems to be exactly what im looking for. How would i write a simple main method to test that? For clarity, that being this code:
    private static final String DELIM = ",";

    /**
    * Compile two files.
    *
    * @param file1 String the input file 1
    * @param file2 String the input file 2
    * @param file3 String the output file
    * @throws IOException error in reading/writing
    */
    public void compileFiles(String file1, String file2, String file3) throws
    IOException {
    BufferedReader reader1 = new BufferedReader(new InputStreamReader(
    new FileInputStream(file1)));
    BufferedReader reader2 = new BufferedReader(new InputStreamReader(
    new FileInputStream(file2)));

    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
    new FileOutputStream(file3)));
    String line1 = reader1.readLine();
    String line2 = reader2.readLine();
    while (line1 != null && line2 != null) {
    writer.write(compileLines(line1, line2));
    writer.newLine();
    line1 = reader1.readLine();
    line2 = reader2.readLine();
    }
    reader1.close();
    reader2.close();
    writer.close();
    }

    private String compileLines(String line1, String line2) {
    StringTokenizer tok1 = new StringTokenizer(line1, DELIM);
    StringTokenizer tok2 = new StringTokenizer(line2, DELIM);
    String name = tok1.nextToken();
    String number = tok1.nextToken();
    String scheme = tok1.nextToken();
    // ignore number
    tok2.nextToken();
    String date = tok2.nextToken();
    String value = tok2.nextToken();

    StringBuffer buffer = new StringBuffer();
    buffer.append(name);
    buffer.append(DELIM);
    buffer.append(number);
    buffer.append(DELIM);
    buffer.append(date);
    buffer.append(DELIM);
    buffer.append(scheme);
    buffer.append(DELIM);
    buffer.append(value);
    return buffer.toString();
    }

    Thanks again

  4. #4
    Join Date
    Jul 2005
    Location
    SW MO, USA
    Posts
    299
    >How would i write a simple main method to test that?
    What is the "that" you want to test?

    What is the purpose of the code you posted? It looks like its merging fields from two files into a third file.

  5. #5
    Join Date
    Oct 2005
    Posts
    6
    I have C# one , it may help you , C# Syntax is almost like Java :

    See the code here : http://www.smartvi.com/webservices.htm

    Regards

Similar Threads

  1. Read Large Text Files
    By Hutty in forum ASP.NET
    Replies: 0
    Last Post: 09-27-2005, 04:11 PM
  2. Replies: 2
    Last Post: 02-28-2002, 10:53 AM
  3. Script for scrolling
    By Mark in forum Web
    Replies: 3
    Last Post: 08-30-2001, 11:45 AM
  4. Double Text 1.0
    By George Gilbert in forum vb.announcements
    Replies: 0
    Last Post: 08-19-2001, 11:34 AM
  5. Can the ADO Text Driver Update Text Files?
    By Larry Rebich in forum VB Classic
    Replies: 1
    Last Post: 12-15-2000, 10:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center