Unit 6 Java Programmingg Question Answer - Copy - Copy-1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216

UNIT6

SubjectName:Java Programming ModelAnswer


SubjectCode:22412

1 2M
Draw the hierarchy of stream
classes.
2M-
Correct
diagram

2 Write a program to read a file (Use character stream) 4M

import java.io.FileWriter; 4M (for


import java.io.IOException; correct
public class IOStreamsExample { program
public static void main(String args[]) throws IOException { and
//Creating FileReader object logic)
File file = new File("D:/myFile.txt");
FileReader reader = new FileReader(file);
char chars[] = new char[(int) file.length()];
//Reading data from the file
reader.read(chars);
//Writing data to another file

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
File out = new File("D:/CopyOfmyFile.txt");
FileWriter writer = new FileWriter(out);
//Writing data to the file
writer.write(chars);
writer.flush();
System.out.println("Data successfully written in the specified file");
}
}
3 Write any four methods of file class with their use. (summer 22 for2M) 4M

One
method
1M

4 Write a program to copy contents of one file to another. (summer 22) 4M

import java.io.*; Correct


class copyf program-
{ 4M

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
public static void main(String args[]) throws IOException
{
BufferedReader in=null;
BufferedWriter out=null;
try
{
in=new BufferedReader(new FileReader("input.txt"));
out=new BufferedWriter(new FileWriter("output.txt"));
int c;
while((c=in.read())!=-1)
{
out.write(c);
}
System.out.println("File copied successfully");
}
finally
{
if(in!=null)
{
in.close();
}
if(out!=null)
{
out.close();
}
}
}
}
5 List the methods of File Input Stream Class. 2M
void close() Any Two
• int read() Each
• int read(byte[] b) for 1
• read(byte[] b, int off, int len) Mark
• int available()
6 Explain the following classes. 4M
i)Byte stream class
ii)Character Stream Class
i)Byte stream class: 2M for
1) InputStream and OutputStream are designed for byte any two
streams points
2) Use the byte stream classes when working with bytes or other
binary objects.

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
3) Input Stream is an abstract class that defines Java’s model of
streaming byte input
4)The Input stream class defines methods for performing input
function such as reading bytes, closing streams, Marking
position in stream.
5) Output Stream is an abstract class that defines streaming byte
output.
6) The output stream class defines methods for performing
output function such as writing bytes, closing streams
ii)Character Stream Class:
1. Reader and Writer are designed for character streams.
2. Use character stream classes when working with characters or
strings.
3. Writer stream classes are designed to write characters.
4. Reader stream classes are designed to read characters.
5The two subclasses used for handling characters in file are
FileReader (for reading characters) and FileWriter (for writing
characters).
7 Write a program to perform following task 6M
(i) Create a text file and store data in it.
(ii) Count number of lines and words in that file.
import java.util.*; Create
import java.io.*; file and
class Model6B store
{ data :
public static void main(String[] args) throws Exception 3M,
{ Get lines
int lineCount=0, wordCount=0; and
String line = ""; word
BufferedReader br1 = new BufferedReader(new count :
InputStreamReader(System.in)); 3M)
FileWriter fw = new FileWriter("Sample.txt"); (Any
//create text file for writing other
System.out.println("Enter data to be inserted in logic
file: "); can be
String fileData = br1.readLine(); consider
fw.write(fileData); ed )
fw.close();
BufferedReader br = new BufferedReader(new
FileReader("Sample.txt"));
while ((line = br.readLine()) != null)
{

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
lineCount++; // no of lines count
String[] words = line.split(" ");
wordCount = wordCount + words.length;
// no of words count
}
System.out.println("Number of lines is : " +
lineCount);
System.out.println("Number of words is : " +
wordCount);
}
8 Define stream class. List its types.(summer 23for 2M) 2M
Definition of stream class: Definitio
An I/O Stream represents an input source or an output destination. A n 1M
stream can represent many different kinds of sources and Types
destinations, including disk files, devices, other programs, and 1M
memory arrays. Streams support many different kinds of data,
including simple bytes, primitive data types, localized characters, and
objects. Java’s stream based I/O is built upon four abstract classes:
InputStream, OutputStream, Reader, Writer.
Types of stream classes:
i. Byte stream classes
ii. Character stream classes
9 Distinguish between Input stream class and output stream class. 4M
Java I/O (Input and Output) is used to process the input and produce Any
the output. four
Java uses the concept of a stream to make I/O operation fast. The points
java.io package contains all the classes required for input and output for input
operations. A stream is a sequence of data. In Java, a stream is stream
composed of bytes. class
and
output
stream
class 1M
each

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
10 Write a program to count number of words from a text file using 4M
stream classes(Note : Any other relevant logic shall be considered)(summer
22)
import java.io.*; Correct
public class FileWordCount program
{ 4M
public static void main(String are[]) throws IOException
{
File f1 = new File("input.txt");
int wc=0;
FileReader fr = new FileReader (f1);
int c=0;
try
{
while(c!=-1)
{
c=fr.read();
if(c==(char)' ')
wc++;
}
System.out.println("Number of words :"+(wc+1));
}
finally
{
if(fr!=null)
fr.close();
}
}
}
11 Write a program to copy content of one file to another file. 4M
class fileCopy Correct
{ logic 2M
public static void main(String args[]) throws IOException Correct
{ Syntax
FileInputStream in= new FileInputStream("input.txt"); 2M
FileOutputStream out= new FileOutputStream("output.txt");
int c=0;
try
{
while(c!=-1)
{
c=in.read();

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
out.write(c);
}
System.out.println("File copied to output.txt....");
}
finally
{
if(in!=null)
in.close();
if(out!=null)
out.close();
}
}
}
12 Give syntax to open a file using InputStream class 2M
Java FileInputStream class is used to open and read a file. We can open and
read a file by using the constructor of the FileInputStream class. The signature
of the constructor is:

1. public FileInputStream(File file) throws FileNotFoundException

13 Write a program to read a file and then count number of words 4M


For ans refer question number 2 and 10

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
14 Differentiate between Byte Stream class and Character Stream class. 4M

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216

15 write a program to append content to a file into another file in java 4M


import java.io.*; Correct
program
public class myProgram { containin
public static void main(String[] args) throws IOException { g 4M

// Define two filenames:


String permFile = "C:\tmp\permFile.txt";
String tmpFile = "C:\tmp\tmpFile.txt";

appendFiles appender = new appendFiles(permFile, tmpFile);


}
}

public class appendFiles {


public static void appendFiles(String permFile, String tmpFile) throws
IOException {
// Appends file "tmpFile" to the end of "permFile"
// Code adapted from:
https://www.daniweb.com/software-development/java/threads/44508/appendi
ng-two-java-text-files

try {
// create a writer for permFile
BufferedWriter out = new BufferedWriter(new FileWriter(permFile,
true));
// create a reader for tmpFile
BufferedReader in = new BufferedReader(new FileReader(tmpFile));
String str;
while ((str = in.readLine()) != null) {
out.write(str);
}
in.close();
out.close();
} catch (IOException e) {
}
}
}

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
16 Write a program for reading and writing character to andfrom the given 4M
files using character stream classes
Import java.io.*;
Public class CharacterStreamExample{
Public static void main(String[]args){
String inputFile=”input.txt”;
String outputFile=”output.txt”;
Try{
FileReader reader=new FileReader(inputFile);
FileWriter writer=new FileWriter(outputFile);
// Read characters from input file and write them to output file
Int character;
While((character=reader.read())!=-1){
Writer.write(character);
}
System.out.println(“File copied successfully”);
}catch(IOException e){
System.out.println(“An error occurred:”+e.getMessage());
}}}

17 Enlist types of Byte strem class and describe input stream class and 4M
output stream class
ByteStream classes are used to read bytes from the input stream and write
bytes to the output stream. In other words, we can say that ByteStream classes
read/write the data of 8-bits. We can store video, audio, characters, etc., by
using ByteStream classes. These classes are part of the java.io package.
The ByteStream classes are divided into two types of classes, i.e., InputStream
and OutputStream.

InputStream Class

The InputStream class provides methods to read bytes from a file, console or
memory. It is an abstract class and can't be instantiated; however, various
classes inherit the InputStream class and override its methods. The methods of
InputStream class are given in the following table.

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216

OutputStream Class

The OutputStream is an abstract class that is used to write 8-bit bytes to the
stream. It is the superclass of all the output stream classes. This class can't be
instantiated; however, it is inherited by various subclasses

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216

18 enlist any four methods of file input stream class and give syntax of any 4M
two methods

Syntax of methods
read ():
ch=fileInputStream.read();
Example of read()
1. import java.io.FileInputStream;
2. public class DataStreamExample {
3. public static void main(String args[]){
4. try{
5. FileInputStream fin=new FileInputStream("D:\\testout.txt");
6. int i=fin.read();
7. System.out.print((char)i);
8.
9. fin.close();
10. }catch(Exception e){System.out.println(e);}
11. }
12. }

close():
FileInputStream object( fin).close();

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
Example of close()

public static void main(String args[]) {

try {

// Suppose, the input.txt file contains the


following text

// This is a line of text inside the file.

FileInputStream input = new


FileInputStream("input.txt");

// Skips the 5 bytes

input.skip(5);

System.out.println("Input stream after skipping 5


bytes:");

// Reads the first byte

int i = input.read();

while (i != -1) {

System.out.print((char) i);

// Reads next byte from the file

i = input.read();

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
// close() method

input.close();

catch (Exception e) {

e.getStackTrace();

Prof.Neha K Pavitrakar ShreeRamchandraCollegeofEngineering Page


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
UNIT

Prof Pavitrakar N.K ShreeRamchandraCollegeofEngineering Page1


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
UNIT

Prof Pavitrakar N.K ShreeRamchandraCollegeofEngineering Page1


SHREERAMCHANDRACOLLEGEOF ENGINEERING, LONIKAND,PUNE412216
UNIT

Prof Pavitrakar N.K ShreeRamchandraCollegeofEngineering Page1

You might also like