10_Files IO and Serialization - 2.pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Document Details

LuxuryAbundance

Uploaded by LuxuryAbundance

Algonquin College

Tags

java programming file input/output

Full Transcript

CST8132 Object Oriented Programming File I/O Dr. James Mwangi Computer Engineering Technology - Computing Science Email: [email protected]...

CST8132 Object Oriented Programming File I/O Dr. James Mwangi Computer Engineering Technology - Computing Science Email: [email protected] School of ICT: CETCS / AP Jan 20, 2023 Welcome to Java OOP James Mwangi PhD. Professor, Computer Engineering Technology – Computer Science [email protected] Warm up Question: 2 minutes: What is a computer file? How is it different from a variable and array? List 2 basic types of computer files. What kind of data is stored in a computer file? 3 Objectives – Week 10 File Input/Output Array & variable: temporary data stores ✓Scanner Data is lost when variable goes out of scope or program ✓Formatter terminates. File - Permanent data storage. ✓Serialization – read and write objects Holds persistent data ✓Lab Two types of files: Text files and Binary files 4 Files and Streams Each file is viewed as a stream of bytes by Java. End-of-file marker provided by operating system to mark the end Byte-based streams: input and ouput data in byte format ( creates binary files) Character-based streams: input and output data in character format. Each character is 2 bytes.(creates text files) Packages: import java.io package import java.nio. Package has classes and interfaces to get file and directory information: path, directoryStream, paths, files NIO Classes and Interfaces import java.nio. Package has classes and interfaces to get file and directory information: Path interface - implemented by classes whose objects represent a file or directory directoryStream interface – implemented by classes to loop thro directory contents Paths class – has static methods that get Path object that reps file or dir location Files class - has static methods for manipulating files: create, copy and delete files CSV File (.csv) vs Text file (.txt) CSV file Text file comma-separator ( , ) Separators ( , : ; ) Name, Age, City Name; Age; City John Doe, 30, New York John Doe; 30; New York Jane Smith, 25, San Francisco Jane Smith; 25; San Francisco Bob Johnson, 35, Chicago Bob Johnson; 35; Chicago Alice Williams, 28, Los Angeles Alice Williams; 28; Los Angeles Each subsequent line represents a data row. Values within a row are separated by commas. Reading a CSV File Read text files – Scanner Open the Input File private s t a t i c Scanner input; 33 Read text files – Scanner Read Info from the Input Text File studentInput.txt 44 Read text files – Scanner Close the Input File 55 Read Text file - Scanner 6 Write text files - Formatter Outputs formatted Strings to the specified stream Accepts the name of the file, including its path (if path not provided, assumes the location from which the program was executed) If the file does not exist, it will be created If the file exists, its content will be truncated 77 Write text files – Formatter - Example 88 Write text files – Formatter - Example 97 A few Possible Exceptions SecurityException: occurs if the user does not have permission to write data to the file FileNotFoundException: occurs if the file does not exist and a new file cannot be created NoSuchElementException: if the data being read by a Scanner method is in the wrong format or if there is no more data to input FormatterClosedException: if the Formatter is closed when you attempt to output 18 0 System.exit Static method System.exit terminates an application An argument of 0 indicates successful program termination A non-zero value normally indicateserror PowerPoint Presentation Title Date 119 Scanner and Formatter Examples – In Eclipse Writing multiple lines into an Output file Writing the elements of an ArrayList into an Outputfile 10 2 Example to Create a File Pearson Education Example to read a File Pearson Education Object Serialization To read an entire object from or write an entire object to a file, Java provides object serialization. A serialized object is represented as a sequence of bytes that includes the object’s data and its type information. After a serialized object has been written into a file, it can be read from the file and deserialized to recreate the object in memory. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 21 Source: geeksforgeeks Object Serialization (cont.) Classes ObjectInputStream and ObjectOutputStream (package java.io). which respectively implement the ObjectInput and ObjectOutputinterfaces, enable entire objects to be read from or written to a stream. To use serialization with files, initialize ObjectInputStream and ObjectOutputStream objects that read from and write to files. © Copyright 1992-2015 by Pearson Education, Inc. All Rights Reserved. 23 Writing Objects to File 24 Writing Objects to File 25 Reading Objects from File 26 Summary Files Input stream vs Output stream Scanner Formatter Serialization – reading and writing objects 27

Use Quizgecko on...
Browser
Browser