Page 351 - computer science (868) class 11
P. 351
Step 3: Read data to the file using different read methods for different data types inside a try-catch block. When the end of file is
met, an EOFException is generated which is handled by the catch block.
Step 4: Close all the stream objects using close() method.
3. Write the steps to write data to a character file.
Ans. Step 1: Create an object of FileWriter class and connect it with the physical file in the disk. If the file does not exist, then it is
created by default.
Step 2: Connect the FileWriter object with the BufferedWriter object. Buffers are the temporary storage areas where the output
stream is first stored.
Step 3: Now, link the BufferedWriter object with the PrintWriter object for writing characters to the text file.
Step 4: Write the text onto the file by using print() or println() method.
Step 5: Lastly, close all the stream objects using the close() method.
4. Explain the three parts of System.out.println().
Ans. • System: It is a final class (a class that does allow other classes to inherit its features) defined in java.lang package.
• out: It is a class variable of PrintStream type, which is a public static member of the System class. Since it is a static member, it
is accessed using the class name, hence System.out is referenced.
• print() or println(): These are public methods of PrintStream class. Since out is of PrintStream type, it is used to call the
methods print() and println().
5. Define a class Stock having the following class description.
Data Members
productC : Product code of product type integer
productN : Product name of String type
price : Product price of type double
quantity : Quantity in stock of type int
reorder : Reorder level of type int. When quantity in stock falls below reorder level, fresh order for buying the
item is to be placed.
Member Methods
void createFile() : Creates a binary file Product.dat having the fields mentioned above as data members
void readRec() : Reads and prints all the records in the file Product.dat
void printReorder() : Prints details of the items which are below reorder level
void editPrice() : Updates the price of an item based on the item number
void delRec() : Deletes an item from stock based on the item number
void addItem() : Increases the quantity of the item in stock when a new item is purchased
void sellItem() : Decreases the quantity of the item in stock when an item is sold
static void main() : Writes a menu-driven program to perform the above file operations
Ans. import java.util.*;
import java.io.*;
class Stock
{
String productN;
int productC,quantity,reorder;
double price;
Scanner sc=new Scanner(System.in);
void createFile() throws IOException // Creating file Product.dat
{
FileOutputStream fout=new FileOutputStream("Product.dat",true);
DataOutputStream dout=new DataOutputStream(fout);
char ch='y';
while(ch=='y' || ch=='Y')
{
System.out.println("Enter product code");
349
Basic Input/Output 349

