473,387 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to compare contents of two files in java?

1
i want a code in java in order to compare contents of two files in java
Sep 8 '10 #1
4 5954
Nepomuk
3,112 Expert 2GB
Hi afroz!

We won't write that code for you, but we can help you write it yourself. But first of all, we need to know what the output should be. I can think of two possibilities:
  1. The output is either "true" or "false", depending on if they are the same or not.
  2. The differences are listed, maybe with some detail. A bit like diff maybe.
No doubt, the second one is much more difficult than the first.

Some general advice: We have an article on reading (and writing to) files here, you should read that.

Greetings,
Nepomuk
Sep 8 '10 #2
Hi,
I'm a java learner. Would you please clarify what are those differences and what are your expectations. And what type of logic would you use? I'm not understanding the case as of now.

Regards :-)
Sep 11 '10 #3
Nepomuk
3,112 Expert 2GB
Hi!

Although you're not the original poster, I'd be happy to explain. ^^

So, first of all, let's assume you have two files you want to compare like these ones:
  • file1.txt:
    Expand|Select|Wrap|Line Numbers
    1. This is
    2. my first
    3. file
  • file2.txt:
    Expand|Select|Wrap|Line Numbers
    1. This is
    2. my second
    3. file
Now, the first attempt would just compare "are the files identical?" which will of course give you "no" (or "false") as an answer. The way to do this would be to read the two files at the same time and compare if you're reading the same in both files.

The second attempt would give you an output something like
Expand|Select|Wrap|Line Numbers
  1. < my first
  2. > my second
Which means "compared to the first file, the second file had the line 'my first' removed and had the line 'my second' inserted." This kind of comparison is much more complex, as if you were to change file2.txt to
Expand|Select|Wrap|Line Numbers
  1. This is
  2. my second
  3. absolutely brilliant
  4. file
the output would be something like
Expand|Select|Wrap|Line Numbers
  1. < my first
  2. > my second
  3. > absolutely brilliant
so it can even recognise that line 3 in file1.txt and line 4 in file2.txt are the same. Now to do something like that, you'll probably have to read in one of the files line by line, save them to some kind of container and then read the other file line by line and check, if the line you just read was already in the first file. So, much more complicated.

Greetings,
Nepomuk
Sep 12 '10 #4
Oh, thanks a lot...
Would you please take some time to solve my tomcat problem threaded in the same forum as well...!

Regards
xploreraj :D
Sep 12 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: david | last post by:
hi: The file can be PDF or Word format. Any help? thx
2
by: kids_pro | last post by:
What is the best method for comparing 2 file? I want to create a utilities to syncronize 2 folders that perform function similiar to robocopy.exe Thanks, kids
1
by: rajesh | last post by:
hi all i want to compare two files,one is a xml file which has some certain rules,and the other one is a "pl/sql" file. i want to check each and every line of the "pl/sql" file which should...
5
by: drabee | last post by:
Please help 2 things: 1-I need c# code to compare 2 audio files .or any other .net code 2-code to receive bluetooth file from mobile and save it using .net code
2
by: Twinkle | last post by:
HI guy I am using This Code For Compare Two Files at Button Click Event. if user select Data from richtextbox1 and click the Compare Button then it will match with second richtextbox2 data. this...
6
by: yinglcs | last post by:
Hi, i have 2 files which are different (1 line difference): $ diff groupresult20070226190027.xml groupresult20070226190027-2.xml 5c5 < x:22 y:516 w:740 h:120 area: --- But when I use the...
1
by: anithas | last post by:
I need to write a C program to compare two files not in the same sequence. ie. Take one line from first file....compare with all lines in second file....If there is no match....put it in a third...
26
by: CodeTilYaDrop | last post by:
Does anyone know how to compare contents of two ArrayLists? I want to compare them to see if they match up. Do you have an example or could you point me in the right direction. I know I need to...
1
by: raj85 | last post by:
i want to compare 2 same files which is located at different location. i olready created 2 file browser button. now i juz need c# code to do comparing text of the both files. it muct check both...
2
by: ksamir2004 | last post by:
How to compare two files in java & uncommon text should print in text file.. thanks Samir
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.