Preview File1
Preview File1
2
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY
Course Objectives
To describe programming, algorithm and its structure, sequences, loops and control flow structure.
To illustrate the structured and objective oriented programming, pointer declaration and initialization.
To demonstrate the applications of encapsulation and data hiding, specifies, constructors and inheritance.
To analyze various types of aggregation, composition, overriding, latch and C++
Course Topics / Major Contents
Introduction to object oriented design, history and advantages of object oriented design, introduction to object oriented
programming
concepts, classes, objects, data encapsulation, constructors, destructors, access modifiers, const vs non-const functions, static
data
members & functions, function overloading, operator overloading, identification of classes and their relationships, composition,
aggregation, inheritance, multiple inheritance, polymorphism, abstract classes and interfaces, generic programming concepts,
function & class templates, standard template library, object streams, data and object serialization using object streams,
exception
handling.
//Creates an InputStream
InputStream object1 = new FileInputStream();
import java.io.FileOutputStream;
import java.io.OutputStream;
try {
OutputStream out = new FileOutputStream("output.txt");
// Converts the string into bytes
byte[] dataBytes = data.getBytes();
// Writes data to the output stream
out.write(dataBytes);
System.out.println("Data is written to the file.");
// Closes the output stream
out.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
In the above example, we have created an output stream using the FileOutputStream class.
The output stream is now linked with the file output.txt.
OutputStream out = new FileOutputStream("output.txt");
To write data to the output.txt file, we have implemented these methods.
output.write(); // To write data to the file
output.close(); // To close the output stream
When we run the program, the output.txt file is filled with the following content.
This is a line of text inside the file.
// Creates a Reader
Reader input = new FileReader();
Here, we have created a reader using the FileReader class. It is because Reader is an
abstract class. Hence we cannot create an object of Reader.
class Main {
public static void main(String[] args) {
try {
Reader input = new FileReader("input.txt");
System.out.println("Is there data in the stream? " +
input.ready());
input.read(array);
System.out.println("Data in the stream:");
System.out.println(array);
input.close();
}
class Main {
public static void main(String[] args) {
int data1 = 5;
String data2 = "This is programiz";
In the above example, we have used the readInt() and readObject() method to read
integer data and object data from the file.
Here, we have used the ObjectOutputStream to write data to the file. We then read the
data from the file using the ObjectInputStream.
String name;
String breed;
// Creates an ObjectInputStream
ObjectInputStream input = new ObjectInputStream(fileStream);
output.close();
input.close();
}
In the above example, we have created an object output stream named objStream that is
linked with the file output stream named fileStream.
class Main {
public static void main(String[] args) {
int data1 = 5;
String data2 = "This is programiz";
// Creates an ObjectOutputStream
ObjectOutputStream output = new ObjectOutputStream(file);
output.close();
objStream.close();
}
In the above example, we have used the readInt() method and readObject() method to
read an integer data and object data from the files.
Here, we have used the ObjectOutputStream to write data to the file. We then read the
data from the file using the ObjectInputStream.
String name;
String breed;
// Creates an ObjectOutputStream
ObjectOutputStream objOut = new ObjectOutputStream(fileOut);
// Writes objects to the output stream
objOut.writeObject(dog1);
// Reads the object
objOut.close();
objIn.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY
Example 2: Java ObjectOutputStream
Output
Dog Name: Tyson
Dog Breed: Labrador
Methods Descriptions
You
.
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 86
Thank
You
.
DEPARTMENT OF COMPUTING, FCIT, INDUS UNIVERSITY 86