Full Code Listing - Reading from a character based file using the FileReader

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

package com.oneyour.io.file;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author deepak
 * A simple program that demonstrates reading from file
 * using a File Reader.
 */

public class ReadingAFile{

    private String filePath = "readFromAFile_TestFile.txt";

    private String carriageReturn = "\n";

    /**
     * The constructor that does all the work.
     * Reads the contents from a file and
     * prints it to the System.out stream
     */

    public ReadingAFile()
    {

        System.out.println("About to read a file");

        // Read the file contents
        String fileContents = readFile(filePath);

        // Display the file contents
        outputFileContents(fileContents);

    }

    /**
     * Read the file specified by the filePath parameter using
     * a FileReader and a BufferedReader
     *
     * @param filePath
     * Path to the file
     *
     * @return
     * Contents of the file
     */

    private String readFile(String filePath)
    {

        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        StringBuilder textFromFile = new StringBuilder();

        try
        {


            fileReader = new FileReader(filePath); // throws FileNotFoundException
            bufferedReader = new BufferedReader(fileReader);

            // Read through the entire file
            String currentLineFromFile = bufferedReader.readLine(); // throws IOException
            while(currentLineFromFile != null)
            {

                // Add a carriage return (line break) to preserve the file formatting.
                textFromFile.append(carriageReturn + currentLineFromFile);
                currentLineFromFile = bufferedReader.readLine(); // throws IOException

            }
            return textFromFile.toString();

        }
        catch (IOException ioException)
        {

            // Failed to read the file
            System.err.println("Problems reading from the file with the path '" + filePath + "'. Program will now exit");
            ioException.printStackTrace();

        }
        finally
        {

            // Good practice: Close the readers to free up any resources.
            try
            {

                if (bufferedReader != null)
                {

                    bufferedReader.close();

                }

                if (fileReader != null)
                {

                    fileReader.close();

                }

            }
            catch(IOException ioExceptionIgnore)
            {

                // Problems while closing the Readers.
                // Nothing much we can do and so we ignore.

            }

        }

        return textFromFile.toString();

    }

    /**
     * Print the file contents to the System.out stream.
     *
     * @param fileContents
     * A String representing the file's contents.
     */

    private void outputFileContents(String fileContents)
    {

        System.out.println("Successfully read the file contents which is printed below.");
        System.out.println(fileContents);

    }

    /**
     * This is the program's entry point
     */

    public static void main(String[] args)
    {

        new ReadingAFile();

    }

}

 

 

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 - Reading from a character based file using the FileReader | Our website now yours! - Currenlty Java focussed.

Full Code Listing - Reading from a character based file using the FileReader

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

package com.oneyour.io.file;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author deepak
 * A simple program that demonstrates reading from file
 * using a File Reader.
 */

public class ReadingAFile{

    private String filePath = "readFromAFile_TestFile.txt";

    private String carriageReturn = "\n";

    /**
     * The constructor that does all the work.
     * Reads the contents from a file and
     * prints it to the System.out stream
     */

    public ReadingAFile()
    {

        System.out.println("About to read a file");

        // Read the file contents
        String fileContents = readFile(filePath);

        // Display the file contents
        outputFileContents(fileContents);

    }

    /**
     * Read the file specified by the filePath parameter using
     * a FileReader and a BufferedReader
     *
     * @param filePath
     * Path to the file
     *
     * @return
     * Contents of the file
     */

    private String readFile(String filePath)
    {

        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        StringBuilder textFromFile = new StringBuilder();

        try
        {


            fileReader = new FileReader(filePath); // throws FileNotFoundException
            bufferedReader = new BufferedReader(fileReader);

            // Read through the entire file
            String currentLineFromFile = bufferedReader.readLine(); // throws IOException
            while(currentLineFromFile != null)
            {

                // Add a carriage return (line break) to preserve the file formatting.
                textFromFile.append(carriageReturn + currentLineFromFile);
                currentLineFromFile = bufferedReader.readLine(); // throws IOException

            }
            return textFromFile.toString();

        }
        catch (IOException ioException)
        {

            // Failed to read the file
            System.err.println("Problems reading from the file with the path '" + filePath + "'. Program will now exit");
            ioException.printStackTrace();

        }
        finally
        {

            // Good practice: Close the readers to free up any resources.
            try
            {

                if (bufferedReader != null)
                {

                    bufferedReader.close();

                }

                if (fileReader != null)
                {

                    fileReader.close();

                }

            }
            catch(IOException ioExceptionIgnore)
            {

                // Problems while closing the Readers.
                // Nothing much we can do and so we ignore.

            }

        }

        return textFromFile.toString();

    }

    /**
     * Print the file contents to the System.out stream.
     *
     * @param fileContents
     * A String representing the file's contents.
     */

    private void outputFileContents(String fileContents)
    {

        System.out.println("Successfully read the file contents which is printed below.");
        System.out.println(fileContents);

    }

    /**
     * This is the program's entry point
     */

    public static void main(String[] args)
    {

        new ReadingAFile();

    }

}

 

 

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