JavaDeploy
SiteMap
Readers Writers
Java Streams
Java Programming
Java Language
Java Input/Output
«Prev
Next»
Java Language
Packages Interfaces
Java Classes - Quiz
Java Graphics
Java Graphics - Quiz
Java Graphics Conclusion
122
MultiThreaded Programming
Multithreaded Programming Basics
Thread Significance Assessment
Create Use Thread Class
Implement Runnable Interface
Sleeping Threads
Thread Synchronization
wait notify Methods
Multithreaded Programming - Conclusion
Set Daemon Threads
Multithreaded Programming - Quiz
Multithreading Fundamentals - Quiz
Handling Exceptions
Understanding Exceptions
Throwing Exception
Catching Exceptions
Catching Exceptions
Custom Exceptions
finally Clause
Handling Exceptions Conclusion
Java Input Output
Create Reader Streams
122
Reading from File - Exercise
Objective:
Write an application that counts the number of characters in a file and then displays the count.
Instructions
Write an application called ReadFile that counts the number of characters in a file and then displays the count. The name of the file will be specified by the user as a command-line argument.
There are a number of valid approaches to writing this application. Perhaps the simplest solution is to create an instance of
FileInputStream
(passing the name of the file in the constructor) and then counting the number of times you can invoke
read()
until the end of file is reached. The
read()
method returns -1 to indicate the end of a file. You are welcome to use this approach or to try another.
Exercise scoring
The full credit for this exercise is 5 points. To receive full credit, you will need to successfully create the source code for the application. You will then submit your source code.
What to submit
In the text box below, cut and paste the source code for ReadFile. Click, the
Submit
button to submit the code.