




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A comprehensive guide on file input and output operations in object-oriented programming, specifically in java. It covers the concept of file input and output, reading from and writing into text files, and exception handling. The document also includes examples and references to relevant textbooks and resources.
Typology: Cheat Sheet
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Description This is the final topic in this course. The topic aims at exploring how file input and output can be performed in Object-oriented Programming languages, particularly in Java. This may not necessarily be a pure object-oriented programming concept. However, it is a major aspect of programming. The process of reading from and writing into a text file will be explored. This will form a better foundation to an advanced course on Java Programming that will cover Java to database connectivity. Learning Outcomes By the end of this topic, you will be able to: ▪ Describe the concept of file input and output as applied in computer programming. ▪ Demonstrate how to read from a text using a programming language. ▪ Demonstrate how to write into a text using a programming language. Overview of File Input and Output Part of computer programming entails being able to store data in some form of files. This may not necessarily be a pure object-oriented concept. It is however a major aspect of programming worth exploring. A file in a computer may be perceived as a collection of information stored on a nonvolatile computing system device [1]. Nonvolatility in this case means that the information is somehow permanent such that it is not lost when the device holding it loses power [1]. Some examples of permanent storage devices include hard disks, USB drives, compact disks, among others. According to Farrell [1], files can be categorized by the way they store data. Thus, based on this, some of the file categories include text files, binary files, data files, program or application files [1]. ▪ Text file – a computer file structured as a sequence of lines of electronic text. Normally saved with a .txt file extension in the Windows Operating system. ▪ Binary file – a computer file that contains non-textual data. Binary file content is usually in a format that can only be interpreted by specific programs or understandable to specific hardware processors.
Figure 2. A Java Program that Read the Text File Sample Data Writing Information into a File Writing to a text file using Java makes use of the PrintWriter class. It is a fairly straight forward process. The following steps summarises the process of writing to a file
Figure 5 shows the initially text file, this time round with the data that was written into it following successful program execution. Figure 5. A Text File with Data Following Program Execution It is worth noting that the concept of exception handling has been applied in both examples by appending the “throws FileNotFoundException” statement at the end of the main() method definition. This is due to the fact that an exception relating to a file not being found is expected. Note that the try … catch … finally approach could still have applied in this context. Summary The topic has discussed the various types of computer files. Regardless of the type of file, a number of operations can be performed on files, including, opening a file, writing into a file, reading from a file, closing a file, and deleting a file. Thus, the topic has demonstrated the process of reading from a text file, writing into a text file, and closing a text file. The concept of exception handling has been applied, but in a slightly different manner. Check Points
Core Textbooks