Java tutorial
//package com.java2s; /** * Copyright 2014 Microsoft Open Technologies Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.*; import java.net.URL; import java.util.Scanner; public class Main { /** * This API compares if two files content is identical. It ignores extra * spaces and new lines while comparing * * @param sourceFile * @param destFile * @return * @throws Exception */ public static boolean isFilesIdentical(URL sourceFile, File destFile) throws Exception { try { Scanner sourceFileScanner = new Scanner(sourceFile.openStream()); Scanner destFileScanner = new Scanner(destFile); while (sourceFileScanner.hasNext()) { /* * If source file is having next token then destination file * also should have next token, else they are not identical. */ if (!destFileScanner.hasNext()) { destFileScanner.close(); sourceFileScanner.close(); return false; } if (!sourceFileScanner.next().equals(destFileScanner.next())) { sourceFileScanner.close(); destFileScanner.close(); return false; } } /* * Handling the case where source file is empty and destination file * is having text */ if (destFileScanner.hasNext()) { destFileScanner.close(); sourceFileScanner.close(); return false; } else { destFileScanner.close(); sourceFileScanner.close(); return true; } } catch (Exception e) { e.printStackTrace(); throw e; } /*finally { sourceFile.close(); }*/ } }