Advanced Programming COMP 20014.1 PDF

Summary

These lecture notes cover fundamental concepts in Java file I/O and streams, including byte-based and character-based streams, input and output streams in the java.io package file operations, and the Java File class. The material is geared towards undergraduate-level computer science students.

Full Transcript

Advanced Programming COMP 20014.1 PPT Disclaimer 2 Lecture 5- Files and Streams Learning objectives In this session you will learn about: Stream in Java Byte-Based and Character-Based Streams Standard Input, Standard Output and Standard Error Streams The...

Advanced Programming COMP 20014.1 PPT Disclaimer 2 Lecture 5- Files and Streams Learning objectives In this session you will learn about: Stream in Java Byte-Based and Character-Based Streams Standard Input, Standard Output and Standard Error Streams The java.io Package Java File Class, InputStream Class, Output Stream Class, Reader and Writer Class File Operations in Java Sequential-access text files - Create Sequential-access text files - Writing data to a sequential text file Stream in Java Java views each file as a sequential stream of bytes. Every operating system provides a mechanism to determine the end of a file, such as an end-of-file marker or a count of the total bytes in the file that’s recorded in a system-maintained administrative data structure. (Ref: Deitel, P, and Deitel, H., 2011) Byte-BasedandCharacter-BasedStreams File streams can be used to input and output data as bytes or characters. Byte-based streams input and output data in its binary format. For example, If the value 5 were being stored using a byte-based stream, it would be stored in the binary format of the numeric value 5, or 101. Character-based streams input and output data as a sequence of characters. For example, If the value 5 were being stored using a character-based stream, it would be stored in the binary format of the character 5, or 00000000 00110101. Files that are created using byte-based streams are referred to as binary files. Files created using character-based streams are referred to as text files. (Ref: Deitel, P, and Deitel, H., 2011) StandardInput,StandardOutputandStandardError Streams In java, 3 streams are created for us automatically. All these streams are attached with console. -System.out: standard output stream -System.in: standard input stream -System.err: standard error stream Class System provides methods setIn, setOut and setErr to redirect the standard input, output and error streams, respectively. (Ref: Deitel, P, and Deitel, H., 2011) The java.io Package Java programs perform file processing by using classes from package java.io.* This package includes definitions for stream classes, such as File-Represents a file or directory path in the filesystem. InputStream-Abstract class representing a byte-based input stream FileInputStream- subclass used for Reading data from a local file OutputStream-Abstract class representing a byte-based output stream FileOutputStream-Used to write data to a file Reader-Abstract class for character-based input FileReader- subclass used for Reading data from a local file Writer -Abstract class for character-based output PrintWriter-used to write formatted data to a file FileWriter-Used to write data to a file (Ref: Deitel, P, and Deitel, H., 2011) Java File Class The File class is an abstract representation of file and directory pathname. A pathname can be either absolute or relative. The File class have several methods for working with directories and files such as creating new directories or files, deleting and renaming directories or files, listing the contents of a directory etc. File f=new File(“Path name”); InputStream &OutputStream Class InputStream class Is an abstract class. It is the super class of all classes representing an input stream of bytes. Java application uses an input stream to read data from a source, it may be a file, an array, peripheral device or socket. Useful methods Method Description read() reads the next byte of data from the input stream. available() returns an estimate of the number of bytes that can be read from the current input stream. close() is used to close the current input stream. InputStream &OutputStream Class OutputStream class Is an abstract class. It is the super class of all classes representing an output stream of bytes. Java application uses an output stream to write data to a destination, it may be a file, an array, peripheral device or socket. Useful methods Method Description write() s used to write a byte to the current output stream. write(byte) is used to write an array of byte to the current output stream. close() is used to close the current output stream. flush() flushes the current output stream. InputStream & OutputStream Java application uses an output stream to write data to a destination; it may be a file, an array, peripheral device or socket. Java application uses an input stream to read data from a source; it may be a file, an array, peripheral device or socket. File Operations in Java Create a File Read from a File Write to a File Delete a File Create a File - use the createNewFile() method. File Operations in Java Write to a File – use the FileOutputStream class along with its write() method in order to write some text to the file. import java.io.FileOutputStream; import java.io.IOException; public class WriteFile { public static void main(String[] args) { String data = "This is a sample text written to the file."; try (FileOutputStream fos = new FileOutputStream("example.txt")) { fos.write(data.getBytes()); System.out.println("Data written to the file successfully."); } catch (IOException e) { System.out.println("An error occurred while writing to the file."); e.printStackTrace(); } } } In-class activity Write a JAVA program to create a new file named “studentDetails”. Using the program, write a sample record consisting of student ID, student Name and marks in 4 modules to the file using OutputStream class. File Operations in Java Read from a File – using FieInputStream class and read() method import java.io.FileInputStream; import java.io.IOException; public class ReadFile { public static void main(String[] args) { try (FileInputStream fis = new FileInputStream("example.txt")) { int content; while ((content = fis.read()) != -1) { System.out.print((char) content); } } catch (IOException e) { System.out.println("An error occurred while reading the file."); e.printStackTrace(); } } } File Operations in Java Delete a File - use the delete() method in order to delete a file. References 1. Roberts, D., 2020. Using Sequential Files - Javabitsnotebook.Com. [online] Mathbits.com. Available at: [Accessed 29 September 2020]. 2. Deitel, P., & Deitel, H. Java how to program. 3. Horstmann, C. Core Java, Volume II-Advanced Features, 11th Edition. 4. Walsh, A., Couch, J., & Steinberg, D. (2000). Java 2 bible. IDG Books Worldwide.

Use Quizgecko on...
Browser
Browser