Full Code Listing - Read multiple character based files in a directory

/**
 * Copyright 2008 www.1your.com
 */

package com.oneyour.io.file;

import java.io.File;

/**
 *
 * @author deepak
 *
 *
 *
 *         A simple program that reads all files with in a specified directory.
 *
 *         Note that this class makes use of the class ReadingAFile
 *
 *         to read each file within the directory.
 */

public class ReadMultipleFilesFromADirectory

{
    /**
     *
     * Note that this directory should be in the project/application
     *
     * root directory.
     *
     *
     * If you want to read all the files from a directory thats
     *
     * outside the root directory, use the absolute path i.e.
     *
     * c:/xyz/multipleFilesDirectory
     */

    private static String multipleFilesDirectory = "multipleFilesDirectory";

    public ReadMultipleFilesFromADirectory()
    {
        readMultipleFilesFromADirectory();
    }

    private static void readMultipleFilesFromADirectory()
    {
        File directory = new File(multipleFilesDirectory);

        // just to double check that this is a directory
        if (directory.isDirectory())
        {
            File[] allFilesWithinThisDirectory = directory.listFiles();
            System.out.println("About to read "
                    + allFilesWithinThisDirectory.length
                    + " within the directory " + directory.getAbsolutePath());
            for (File file : allFilesWithinThisDirectory)
            {
                System.out.println("\n** About to read a file "
                        + file.getName() + " **");
                String fileContents = ReadingAFile.readFile(file);
                System.out.println("The file contents are:");
                System.out.println(fileContents);
            }
        }
    }

    /**
     *
     * @param args
     */

    public static void main(String[] args)
    {
        new ReadMultipleFilesFromADirectory();
    }

}

 

 

Comments

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

Full Code Listing - Read multiple character based files in a directory | Our website now yours! - Currenlty Java focussed.

Full Code Listing - Read multiple character based files in a directory

/**
 * Copyright 2008 www.1your.com
 */

package com.oneyour.io.file;

import java.io.File;

/**
 *
 * @author deepak
 *
 *
 *
 *         A simple program that reads all files with in a specified directory.
 *
 *         Note that this class makes use of the class ReadingAFile
 *
 *         to read each file within the directory.
 */

public class ReadMultipleFilesFromADirectory

{
    /**
     *
     * Note that this directory should be in the project/application
     *
     * root directory.
     *
     *
     * If you want to read all the files from a directory thats
     *
     * outside the root directory, use the absolute path i.e.
     *
     * c:/xyz/multipleFilesDirectory
     */

    private static String multipleFilesDirectory = "multipleFilesDirectory";

    public ReadMultipleFilesFromADirectory()
    {
        readMultipleFilesFromADirectory();
    }

    private static void readMultipleFilesFromADirectory()
    {
        File directory = new File(multipleFilesDirectory);

        // just to double check that this is a directory
        if (directory.isDirectory())
        {
            File[] allFilesWithinThisDirectory = directory.listFiles();
            System.out.println("About to read "
                    + allFilesWithinThisDirectory.length
                    + " within the directory " + directory.getAbsolutePath());
            for (File file : allFilesWithinThisDirectory)
            {
                System.out.println("\n** About to read a file "
                        + file.getName() + " **");
                String fileContents = ReadingAFile.readFile(file);
                System.out.println("The file contents are:");
                System.out.println(fileContents);
            }
        }
    }

    /**
     *
     * @param args
     */

    public static void main(String[] args)
    {
        new ReadMultipleFilesFromADirectory();
    }

}

 

 

Comments

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options