Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

CO_IF_22412_CO6

Mr. Siddhesh Ashok Vaidya, Lecturer, Vidyalankar Polytechnic

Date: 25th February 2021

Learning at your doorstep


Unit 6:
Managing Input/Output Files in Java

Written by

Siddhesh Ashok Vaidya


Lecturer, Department of Computer Engineering (NBA Accredited),
Vidyalankar Polytechnic, Mumbai
Unit Outcome 4: Write program to
demonstrate use of primitive data
types with specified streams.
Learning Outcome 4: Student should
understand the concept of I/O
Exception, Creating Files, Reading and
Writing Byte Stream and Character
Stream Class
What we will learn today

1. Program on IO Exception
2. Program to create file
3. Program to copy contents of one file to another using Byte Stream
4. Program to copy contents of one file to another using Character Stream

Key takeaways
Siddhesh Ashok Vaidya
Creation of Files Lecturer, Department of Computer Engineering (NBA Accredited), Vidyalankar Polytechnic,
Mumbai
Copying contents of files

Page 5 Maharashtra State Board of Technical Education


Learning Objective/Key learning

► Understand concept of creation of files, copying contents of files using Byte Streams and Character
Streams

Page 6 Maharashtra State Board of Technical Education


Concept Explanation: Code for IO Exception

import java.io.*;
public class AddTwoNumbers
{
public static void main(String[] args) throws IOException
{
int num1, num2, sum;
DataInputStream d = new DataInputStream(System.in);
System.out.println("Enter First Number: ");
num1 = Integer.parseInt(d.readLine());
System.out.println("Enter Second Number: ");
num2 = Integer.parseInt(d.readLine());
sum = num1 + num2;
System.out.println("Sum of these numbers: "+sum);
}
}

Page 7 Maharashtra State Board of Technical Education


Concept Explanation: Code for creation of File
import java.io.File;
import java.io.IOException;
public class CreateFile
{
public static void main(String[] args)
{
try
{
File file = new File("C:\\Simple.txt");
if(file.createNewFile())
System.out.println("Success!");
else
System.out.println("Error, file already exists.");
}
catch(IOException ie)
{
ie.printStackTrace();
}
}
}
Page 8 Maharashtra State Board of Technical Education
Concept Explanation: Copying content of one file to another using Byte Stream
import java.io.*; if(out != null)
import java.util.*; {
public class CopyFileSD out.close();
{ }
public static void copyContent(File a, File b) throws Exception }
{ System.out.println("File Copied");
FileInputStream in = new FileInputStream(a); }
FileOutputStream out = new FileOutputStream(b);
try public static void main(String[] args) throws Exception
{ {
int n; Scanner sc = new Scanner(System.in);
while((n = in.read())!= -1) System.out.println("Enter the source filename from where you
{ have to read/copy :");
out.write(n); String a = sc.nextLine();
} File x = new File(a);
} System.out.println("Enter the destination filename where you
finally have to write/paste :");
{ String b = sc.nextLine();
if(in != null) File y = new File(b);
{ copyContent(x, y);
in.close(); }
} }
Page 9 Maharashtra State Board of Technical Education
Concept Explanation: Copying content of one file to another using Byte Stream

Page 10 Maharashtra State Board of Technical Education


Concept Explanation: Copying content of one file to another using Character Stream
import java.io.*;class Copyf while((c=in.read())!=-1)
{ {
public static void main(String args[]) throws IOException out.write(c);
{ }
BufferedReader in=null; System.out.println("File copied successfully");
BufferedWriter out=null; }
try finally
{ {
in=new BufferedReader(new FileReader("FileA")); if(in!=null)
out=new BufferedWriter(new FileWriter("FileB")); {
int c; in.close();
}
if(out!=null)
{
out.close();
}
}
}
Page 11 Maharashtra State Board of Technical Education
}
Concept Explanation: Copying content of one file to another using Character Stream

Page 12 Maharashtra State Board of Technical Education


Summary

 IO Exception

 Creation of Files

 Copying File contents from Source to Destination using Byte Streams

 Copying File contents from Source to Destination using Character Streams

Page 13 Maharashtra State Board of Technical Education

You might also like