Home arrow Java arrow Page 3 - Java Part 5: Input And Output Operations

Java Part 5: Input And Output Operations

In this article, Chris will continue his popular "Java How-To" series and teach us all how to create a simple Java application that will read a file and output its contents to the screen.

Author Info:
By: Chris Noack
Rating: 3 stars3 stars3 stars3 stars3 stars / 30
January 03, 2002
  1. · Java Part 5: Input And Output Operations
  2. · Our file I/O application explained
  3. · Our file I/O application explained (contd.)
  4. · Conclusion

print this article

Java Part 5: Input And Output Operations - Our file I/O application explained (contd.)
(Page 3 of 4 )

String fileName = args[0];

We create a new string object named "fileName". The value of the first index in the "args" array is assigned to the "filename" value. Remember, this will be the name of the file that our application will open for reading.

FileReader fr = new FileReader(fileName);

BufferedReader input = new BufferedReader(fr);

Next, we create two new variables: "fr" and "input". The "fr" variable contains a new file stream (which is used to read the file), and "input" (which is used to access the file stream) now contains a new "BufferedReader" object.

String s = input.readLine();

while( s instanceof String ){


s = input.readLine();


We get the first line from the file using the "readLine" method of our "FileReader" object and store it in a new string variable, "s". The while loop tests the variable "s" to see whether it is actually an instance of a string. If it is, then its value is printed to the screen using the "System.out.println" method. We repeat this loop until the variable "s" is not an instance of a string, which would mean that we have reached the end of the file.


Lastly, we use the "System.exit" method to return control to the operating system. In this case, it returns a value of zero, indicating that our application succeeded. If we wanted to indicate an error, we would return "1" with the "System.exit" method, like this:


Compiling our application

Before we can run our "FileIO.java" file, we need to compile it (refer to part three of this series for the full tutorial on installing the Java SDK). From the command prompt, change to the directory where you saved the "FileIO.java" file, and enter this command:

javac FileIO.java

This will compile our ".java" file into Java byte code, which can them be executed using the Java interpreter, "java", like this:

java FileIO <filename>

Simply replace <filename> with the full path and name of a file on your machine. You should just create a simple text file to test our application.
blog comments powered by Disqus

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials