File Input and Output in Object-Oriented Programming - Java, Cheat Sheet of Programming Languages

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

2023/2024

Uploaded on 03/25/2024

kebaso-mwita
kebaso-mwita 🇰🇪

1 document

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Object Oriented ProgrammingJava 1
Lecture 12
File Input and Output
Dr. Obuhuma James
pf3
pf4
pf5
pf8

Partial preview of the text

Download File Input and Output in Object-Oriented Programming - Java and more Cheat Sheet Programming Languages in PDF only on Docsity!

Object Oriented Programming – Java 1

Lecture 12

File Input and Output

Dr. Obuhuma James

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.

  1. Use the Scanner object to read data from the source file in a similar fashion that input data from the standard input device was done in our earlier topic. In this case, the next(), nextInt(), nextLine, among other methods are put in use. Syntax objectname.methodname();
  2. Close the file using the close() method. Syntax objectname.close(); Reading from a Text File Example Consider a text file called data.txt in Figure 1 consisting of a line of sample data where the data represents first name, last name, age and salary respectively. Figure 1. A Text File with Sample Data Figure 2 shows a Java program that demonstrates how the data in the data.txt file could be read and displayed on the console. Notice the output window that show the data having been successfully read and displayed on the console.

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

  1. Import the necessary class from the java.io package. Syntax import java.io.PrintWriter;
  2. Create an object of the PrintWriter class specifying the destination file. Syntax PrintWriter objectname = new PrintWriter(filename_and_path);
  3. Use the print() or println() methods to write the data into the destination in a similar fashion that data was written on the console in our earlier topic. Syntax objectname.print();

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

  1. Describe the difference among the various types of computer files.
  2. Outline the common operations that can be performed on computer files.
  3. Using an example, demonstrate the process of reading from a data file.
  4. Using an example, demonstrate the process of writing into a data file.
  5. Describe how the concept of exception handling applied to the process of reading from and writing into computer files using Java.

Core Textbooks

  1. Joyce Farrell, Java Programming, 7th Edition. Course Technology, Cengage Learning, 2014, ISBN-13 978- 1 - 285 - 08195 - 3.
  2. Malik, Davender S. JavaTM Programming: From Problem Analysis to Program Design, International Edition, 5th Edition, Cengage Learning. Other Resources
  3. Daniel Liang, Y. "Introduction to Java Programming, Comprehensive." (2011).
  4. Malik, Davender S. JavaTM Programming: From Problem Analysis to Program Design, International Edition, 4th Edition, Cengage Learning, 2011.
  5. Shelly, Gary B., et al. Java programming: comprehensive concepts and techniques. Cengage Learning, 2012. References [1] Farrell, J., Java Programming, 7th Edition. Course Technology, Cengage Learning, 2014, ISBN-13 978- 1 - 285 - 08195 - 3. [2] Malik, D. S., JavaTM Programming: From Problem Analysis to Program Design, International Edition, 5th Edition, Cengage Learning. [3] Sebester, R. W., Concepts of Programming Languages, 1 2 th^ Edition, Pearson, 201 8 , ISBN 0 - 321 - 49362 - 1.