The below program outlines what you need to do. Each Java Development Kit (JDK) version ships a version of the Java tools and utilities. MappedByteBuffer, a representation of the memory area of your file's contents can be created from the channel that you get access from the RandomAccessFile class. Open your files by using the RandomAccessFile class and ask for the channel from this object if you want to memory map the two files. The OS allocates the memory itself and hence the program doesn't consume heap memory. Files that are mapped by the memory are read directly by your operating system. For a very fast and efficient solution, you need to use a highly advanced technique called memory-mapping.
This program is simple in nature and may take up a lot of memory and time to execute. ("File1 has "+line1+" and File2 has "+line2+" at line "+lineNum)
Now Run the java code in your favorite browser instantly. Dont worry about setting up java environment in your local. Its one of the quick, robust, powerful online compilers for java language. Build, Run & Share Java code online using online-javas IDE for free. Public static void main(String args) throws IOExceptionīufferedReader reader1 = new BufferedReader(new FileReader("C:\\file1.txt")) īufferedReader reader2 = new BufferedReader(new FileReader("C:\\file2.txt")) Online Java is a web-based tool powered by ACE code editor. Hence, we will be using a highly advanced technique called memory mapping using RandomAccessFile from java.io package. First, we will be comparing the files using BufferedReader but this way of comparing files is not memory efficient and requires more execution time. You can quickly identify the duplicate lines, which allows you to remove the file entirely. Comparing files in java also can help you to differentiate between the local and the remote files. In this program, we will see how we can compare two different files in Java. Between the two columns, there is an arrow sign. And paste the second code or text file (with whom you want to compare) in the second or right column. Paste the first code or text file in the first or left column. To make the comparison, do the following steps. When you are learning Java, network programming can be a great source of practicing Java. You can also call itcode similarity checker tool.