Get a list of files, and check if any files are missing : Files « File Input Output « Java






Get a list of files, and check if any files are missing

  
/*
 * Copyright (c) Ian F. Darwin, http://www.darwinsys.com/, 1996-2002.
 * All rights reserved. Software written by Ian F. Darwin and others.
 * $Id: LICENSE,v 1.8 2004/02/09 03:33:38 ian Exp $
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * Java, the Duke mascot, and all variants of Sun's Java "steaming coffee
 * cup" logo are trademarks of Sun Microsystems. Sun's, and James Gosling's,
 * pioneering role in inventing and promulgating (and standardizing) the Java 
 * language and environment is gratefully acknowledged.
 * 
 * The pioneering role of Dennis Ritchie and Bjarne Stroustrup, of AT&T, for
 * inventing predecessor languages C and C++ is also gratefully acknowledged.
 */

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Get a list of files, and check if any files are missing.
 * 
 * @author Ian F. Darwin, http://www.darwinsys.com/
 * @version $Id: CheckFiles.java,v 1.3 2004/02/09 03:33:46 ian Exp $
 */
public class CheckFiles {
  public static void main(String[] argv) {
    CheckFiles cf = new CheckFiles();
    System.out.println("CheckFiles starting.");
    cf.getListFromFile();
    cf.getListFromDirectory();
    cf.reportMissingFiles();
    System.out.println("CheckFiles done.");
  }

  public String FILENAME = "filelist.txt";

  protected ArrayList listFromFile;

  protected ArrayList listFromDir = new ArrayList();

  protected void getListFromFile() {
    listFromFile = new ArrayList();
    BufferedReader is;
    try {
      is = new BufferedReader(new FileReader(FILENAME));
      String line;
      while ((line = is.readLine()) != null)
        listFromFile.add(line);
    } catch (FileNotFoundException e) {
      System.err.println("Can't open file list file.");
      return;
    } catch (IOException e) {
      System.err.println("Error reading file list");
      return;
    }
  }

  /** Get list of names from the directory */
  protected void getListFromDirectory() {
    listFromDir = new ArrayList();
    String[] l = new java.io.File(".").list();
    for (int i = 0; i < l.length; i++)
      listFromDir.add(l[i]);
  }

  protected void reportMissingFiles() {
    for (int i = 0; i < listFromFile.size(); i++)
      if (!listFromDir.contains(listFromFile.get(i)))
        System.err.println("File " + listFromFile.get(i) + " missing.");
  }
}


           
         
    
  








Related examples in the same category

1.Create file
2.Create a temporary file
3.Creating a Temporary File and delete it on exit
4.Create a directory (or several directories)
5.Get file size
6.Change last modified time of a file or directory
7.Construct file path
8.Create temporary file with specified extension suffix
9.Create temporary file in specified directory
10.Create new empty file
11.Compare two file paths
12.Delete a file
13.Delete file or directory
14.Deleting a Directory (an empty directory)
15.Delete file or directory when virtual machine terminates
16.Determine File or Directory
17.Determine if a file can be read
18.Determine if a file can be written
19.Determine if file or directory exists
20.Determine if File or Directory is hidden
21.Demonstrate File.
22.Moving a File or Directory to Another Directory
23.Find out the directoryFind out the directory
24.Get all path information from java.io.File
25.Getting an Absolute Filename Path from a Relative Filename Path
26.Getting an Absolute Filename Path from a Relative Filename with Path
27.Getting an Absolute Filename Path from a Relative Filename parent Path
28.Get Absolute path of the file
29.Get File size in bytes
30.Get parent directory as a File object
31.Get a file last modification date
32.File.getCanonicalFile() converts a filename path to a unique canonical form suitable for comparisons.
33.Getting the Parents of a Filename Path
34.Get the parents of an absolute filename path
35.Getting and Setting the Modification Time of a File or Directory
36.Mark file or directory Read Only
37.List Filesystem roots
38.List drives
39.Listing the Directory ContentsListing the Directory Contents
40.Rename file or directory
41.Forcing Updates to a File to the Disk
42.Random FileRandom File
43.Create a directory; all ancestor directories must exist
44.Create a directory; all non-existent ancestor directories are automatically created
45.Getting the Current Working Directory
46.Change a file attribute to writable
47.Data fileData file
48.Output to a text File
49.Choose a FileChoose a File
50.Read data from text file
51.Copy File
52.Querying a File for Information
53.Working with RandomAccessFileWorking with RandomAccessFile
54.Delete a file from within Java
55.Work with temporary files in Java
56.Compare File Dates
57.Sort files base on their last modified date
58.Strings -- extract printable strings from binary file
59.Get extension, path and file nameGet extension, path and file name
60.Read file contents to string using commons-io?
61.Get all xml files by file extension
62.Get icon for file type
63.Change a file attribute to read only
64.Get file extension name
65.Search for files recursively
66.Create a human-readable file size
67.Set file attributes.
68.Format file length in string
69.Return a file with the given filename creating the necessary directories if not present.