Page 2 of 2 FirstFirst 12
Results 21 to 26 of 26
  1. #21
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: FileReader reads all data as int

    Why character by character instead of line by line?

    best way for me to compare character by character from 2 files
    You could read them as bytes and compare them as bytes also.

  2. #22
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: FileReader reads all data as int

    Quote Originally Posted by Norm View Post
    Why character by character instead of line by line?


    You could read them as bytes and compare them as bytes also.
    Well I'd like to have the capability to point out to the user exactly where the changes in the 2 files occur. Currently, my code is
    Java Code:
    import java.io.FileReader;
    import java.io.BufferedReader;
    import java.io.IOException;
    
    public class fileComp {
    	public static void main(String args[]) throws IOException {
    		System.out.println("This program compares 2 files line by line and works well for things like " +
    				"CSV files. It will print any differences between the 2 files.\n---------");
    		BufferedReader original = null, modified = null;
    		
    		try {
    			original = new BufferedReader(new FileReader("1.csv"));
    			modified = new BufferedReader(new FileReader("2.csv"));
    			
    			String line1, line2;
    			int i = 1, changes = 0;
    			
    			while ((line1 = original.readLine()) != null && (line2 = modified.readLine()) != null) {
    				if (line1.contentEquals(line2) == false) {
    					changes++;
    					System.out.println("\nLine " + i + " has changed.\n" +
    							"Old value: " + line1 + "\n" +
    							"New value: " + line2);
    				}
    				i++;
    			}
    			System.out.println("\nRecords changed: " + changes);
    		} finally {
    			if (original == null || modified == null) {
    				System.out.println("The original and/or modified file(s) could not be loaded.");
    			} else {
    				original.close();
    				modified.close();
    			}
    		}
    	}
    }
    It works well, however it only shows the user which LINE the changes occur.

    If I compare byte by byte, I cannot display the part of the file that changed in its original format right? Since it would all be converted to integers?

  3. #23
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: FileReader reads all data as int

    It all depends on what you want the program to do.

  4. #24
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: FileReader reads all data as int

    Quote Originally Posted by Norm View Post
    It all depends on what you want the program to do.
    If I want to print to the console the before and after of the changed content of the file the way a text editor reads it, would the best option be to use BufferedReader and loop through the resultant string until the change is found, and print that change?

  5. #25
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,449
    Rep Power
    25

    Default Re: FileReader reads all data as int

    Yes, reading the files into Strings would make showing the place of the difference easier.
    When two lines were found to be different then you could look at each of them char by char using the charAt method to find the location of the difference.

  6. #26
    fred2028 is offline Member
    Join Date
    Sep 2011
    Posts
    26
    Rep Power
    0

    Default Re: FileReader reads all data as int

    Quote Originally Posted by Norm View Post
    Yes, reading the files into Strings would make showing the place of the difference easier.
    When two lines were found to be different then you could look at each of them char by char using the charAt method to find the location of the difference.
    OK thanks! I really appreciate the quick and informative replies!

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Write a Program that reads two times in military format
    By HPcompaq256 in forum New To Java
    Replies: 10
    Last Post: 02-26-2010, 04:38 AM
  2. add FileReader to GUI
    By VinTiger in forum New To Java
    Replies: 8
    Last Post: 05-11-2009, 12:23 AM
  3. Hosting an Applet on a website which reads a textfile
    By Bomber_Will in forum Java Applets
    Replies: 3
    Last Post: 01-17-2009, 06:48 PM
  4. Replies: 1
    Last Post: 11-12-2008, 03:43 PM
  5. Replies: 0
    Last Post: 11-06-2007, 02:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •