Java input output operation related introduction.

SHOUBHIK1111 15 views 19 slides Aug 05, 2024
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

Java input output


Slide Content

© 2017 Wiprowipro.com confidential1Sensitivity: Internal & Restricted
I/O Operations

Sensitivity: Internal & Restricted
Agenda
Console Operations
File Operations2

© 2017 Wiprowipro.com confidential3Sensitivity: Internal & Restricted
Objectives
At the end of this module, you will be able to:
Understand Console operations
Understand File operations

© 2017 Wiprowipro.com confidential4Sensitivity: Internal & Restricted
Reading & Printing to Console

Sensitivity: Internal & Restricted
•ThepreferredmethodofreadingconsoleinputinJava2istouseacharacterstream
•InputStreamReaderclassactsasabridgebetweenbyteandcharacterstreams
•ConsoleinputisaccomplishedbyreadingfromSystem.in
•Togetacharacter-basedstream,youwrapSystem.ininaBufferedReaderobject
Reading Console Input -Stream Wrapping

© 2017 Wiprowipro.com confidential6Sensitivity: Internal & Restricted
Reading Console Input -Stream Wrapping
TheBufferedReaderclasssupportsabufferedinputstream.Itsmostcommonlyused
constructorisshownasfollows:
BufferedReader(ReaderinputReader)
HereinputReaderisthestreamthatislinkedtotheinstanceofBufferedReaderthatis
beingcreated.Readerisanabstractclass.Oneofitsconcretesubclassesis
InputStreamReader,whichconvertsbytestocharacters.Toobtainan
InputStreamReaderobjectthatislinkedtoSystem.in,usethefollowingconstructor:
InputStreamReader(InputStreaminputStream)

© 2017 Wiprowipro.com confidential7Sensitivity: Internal & Restricted
Reading Console Input -Stream Wrapping
BecauseSystem.inreferstoanobjectoftypeInputStream,itcanbeusedforinputStream.
Puttingitalltogether,thefollowinglineofcodecreatesaBufferedReaderthatisconnected
tothekeyboard,andwhichinturnenablescharacterinputfromabytestreamInputStream
thatisSystem.in).
BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));

Sensitivity: Internal & Restricted
package m10.io;
import java.io.*;
public class BRRead{
public static void main (String args[ ]) throws IOException {
char c;
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println(“Enter Characters, ‘q’ to quit”);
do {
c = (char) br.read( );
System.out.println( c );
}while (c != ‘q’);
}
}
Reading Characters
Refer documentation for
BufferedReader and
InputStreamReader

© 2017 Wiprowipro.com confidential9Sensitivity: Internal & Restricted
Reading Characters
intread()throwsIOException
Whenevertheread()methodiscalled,itreadsacharacterfromtheinputstreamand
returnsanintegervalue.Iftheendofthestreamisencountered,-1isreturned.

Sensitivity: Internal & Restricted
packagem10.io;
importjava.io.*;
publicclassBRReadLine{
publicstaticvoidmain(Stringargs[])throwsIOException{
Stringstr;
BufferedReaderbr=newBufferedReader(new
InputStreamReader(System.in));
System.out.println(“Enter Characters,‘stop’toquit”);
do{
str=br.readLine();
System.out.println (str);
}while(!str.equals(“stop”));
}
}
The above program reads and displays lines of text until you enter the word “stop”.
ReadingStrings

Sensitivity: Internal & Restricted
•print()andprintln()areconsoleoutputmethodsdefinedinPrintStreamclass
•System.outisabytestreamusedtowritebytes
Writing Console Output

© 2017 Wiprowipro.com confidential12Sensitivity: Internal & Restricted
Writing & Reading From File

© 2017 Wiprowipro.com confidential13Sensitivity: Internal & Restricted
Reading&WritingtoFileusingFileReader&FileWriter
TheFileclassisaconvenienceclassforwritingcharacterfiles.TheFileclassdealsdirectly
withfilesandthefilesystem.TheFileclassdoesnotspecifyhowinformationisretrieved
from,orstoredinfiles,itdescribesthepropertiesofafileitself.AFileobjectisusedtoobtain
ormanipulateinformationassociatedwithadiskfile,suchasthepermissions,time,dateand
directorypath.
publicintread()throwsIOException(Readasinglecharacter)
publicintread(char[]cbuf,intoff,intlen)throwsIOException
publicvoidwrite(intc)throwsIOException(Writeasinglecharacter)

Sensitivity: Internal & Restricted
packagem10.io;
importjava.io.*;
publicclassCopy{
publicstaticvoidmain(String[]args)throwsIOException{
FileinputFile=newFile(”Source.txt");
FileoutputFile=newFile(”Target.txt");
FileReaderin=newFileReader(inputFile);
FileWriterout=newFileWriter(outputFile);
intc;
while((c=in.read())!=-1)
out.write(c);
in.close();
out.close();
}
}
Reading&WritingtoFileusingFileReader&FileWriter
Refer documentation for
FileReader and FileWriter

Sensitivity: Internal & Restricted
importjava.io.*;
classCopyFile{
publicstaticvoidmain(Stringargs[])throwsIOException{
inti;
FileInputStream fin;
FileOutputStream fout;
try{
fin=newFileInputStream(args[0]);
}
catch(FileNotFoundException e){
System.out.println("File NotFound");
return;
}
Copyimage
Why can’t we use FileReader and FileWriter here?

Sensitivity: Internal & Restricted
Copyimage(Contd.).
try{
fout=newFileOutputStream(args[1]);
}
catch(IOExceptione){
System.out.println("Error OpeningOutputFile");
return;
}
try{
do{
i=fin.read();
if(i!=-1)
fout.write(i);
}while(i!=-1);
}
catch(IOExceptione){
System.out.println("File Error");
}
fin.close();
fout.close();
}
}

© 2017 Wiprowipro.com confidential17Sensitivity: Internal & Restricted
Copyimage(Contd.).
Torunthisprogram
C:\javaCopyFilesource.bmpdest.bmp
Itwillcopyimagefromsource.bmptodest.bmp

Sensitivity: Internal & Restricted

© 2017 Wiprowipro.com confidential19Sensitivity: Internal & Restricted © 2017 Wiprowipro.com confidential19
Thank You
Tags