Download - I/O in java Part 1
![Page 1: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/1.jpg)
File IO in Java
Prof. AshishSingh Bhatia, [email protected],[email protected], Web: asbspace.in, M:9879009551
September 18, 2012
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 1
![Page 2: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/2.jpg)
AGENDA
Understanding Streams
Streams
Files
File class methods
File class methods -Cont
Programs using File class
Program to rename a file.
Program to delete a file.
Types of Streams
Stream Base IO
Overview of IO Classes
OutputStream of IO
InputStream of IO
Writer of IO
Reader of IOProf. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 2
![Page 3: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/3.jpg)
Understanding Streams
Input Stream
Output Stream
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 3
![Page 4: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/4.jpg)
Streams
I Sequence of bytes.
I Input Stream : Data is received by some device to program.
I Output Stream : Data is sent by the program to device.
I Stream provides a sort of abstraction.
I Recall System.out and System.in
I System.in object of InputStream and System.out andSystem.err is object of PrintStream class.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 4
![Page 5: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/5.jpg)
Streams
I Sequence of bytes.
I Input Stream : Data is received by some device to program.
I Output Stream : Data is sent by the program to device.
I Stream provides a sort of abstraction.
I Recall System.out and System.in
I System.in object of InputStream and System.out andSystem.err is object of PrintStream class.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 5
![Page 6: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/6.jpg)
Streams
I Sequence of bytes.
I Input Stream : Data is received by some device to program.
I Output Stream : Data is sent by the program to device.
I Stream provides a sort of abstraction.
I Recall System.out and System.in
I System.in object of InputStream and System.out andSystem.err is object of PrintStream class.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 6
![Page 7: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/7.jpg)
Streams
I Sequence of bytes.
I Input Stream : Data is received by some device to program.
I Output Stream : Data is sent by the program to device.
I Stream provides a sort of abstraction.
I Recall System.out and System.in
I System.in object of InputStream and System.out andSystem.err is object of PrintStream class.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 7
![Page 8: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/8.jpg)
Streams
I Sequence of bytes.
I Input Stream : Data is received by some device to program.
I Output Stream : Data is sent by the program to device.
I Stream provides a sort of abstraction.
I Recall System.out and System.in
I System.in object of InputStream and System.out andSystem.err is object of PrintStream class.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 8
![Page 9: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/9.jpg)
Streams
I Sequence of bytes.
I Input Stream : Data is received by some device to program.
I Output Stream : Data is sent by the program to device.
I Stream provides a sort of abstraction.
I Recall System.out and System.in
I System.in object of InputStream and System.out andSystem.err is object of PrintStream class.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 9
![Page 10: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/10.jpg)
Files
I File class is provided by java.io package.
I An abstract reprsentation of file and directory pathnames.
I Note File object is not for reading / writing files.
I Used for obtaining information associated with file likepermission, time and date, path.
I Directory too is treated as file and have additional methodlist() to list the filename in directory.
I public File(String pathname)
public File(String parent, String child)
public File(File parent, String child)
public File(URI uri
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 10
![Page 11: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/11.jpg)
Files
I File class is provided by java.io package.
I An abstract reprsentation of file and directory pathnames.
I Note File object is not for reading / writing files.
I Used for obtaining information associated with file likepermission, time and date, path.
I Directory too is treated as file and have additional methodlist() to list the filename in directory.
I public File(String pathname)
public File(String parent, String child)
public File(File parent, String child)
public File(URI uri
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 11
![Page 12: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/12.jpg)
Files
I File class is provided by java.io package.
I An abstract reprsentation of file and directory pathnames.
I Note File object is not for reading / writing files.
I Used for obtaining information associated with file likepermission, time and date, path.
I Directory too is treated as file and have additional methodlist() to list the filename in directory.
I public File(String pathname)
public File(String parent, String child)
public File(File parent, String child)
public File(URI uri
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 12
![Page 13: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/13.jpg)
Files
I File class is provided by java.io package.
I An abstract reprsentation of file and directory pathnames.
I Note File object is not for reading / writing files.
I Used for obtaining information associated with file likepermission, time and date, path.
I Directory too is treated as file and have additional methodlist() to list the filename in directory.
I public File(String pathname)
public File(String parent, String child)
public File(File parent, String child)
public File(URI uri
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 13
![Page 14: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/14.jpg)
Files
I File class is provided by java.io package.
I An abstract reprsentation of file and directory pathnames.
I Note File object is not for reading / writing files.
I Used for obtaining information associated with file likepermission, time and date, path.
I Directory too is treated as file and have additional methodlist() to list the filename in directory.
I public File(String pathname)
public File(String parent, String child)
public File(File parent, String child)
public File(URI uri
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 14
![Page 15: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/15.jpg)
Files
I File class is provided by java.io package.
I An abstract reprsentation of file and directory pathnames.
I Note File object is not for reading / writing files.
I Used for obtaining information associated with file likepermission, time and date, path.
I Directory too is treated as file and have additional methodlist() to list the filename in directory.
I public File(String pathname)
public File(String parent, String child)
public File(File parent, String child)
public File(URI uri
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 15
![Page 16: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/16.jpg)
File class methods
I boolean canRead()
I boolean canWrite()
I boolean cancreateNewFile()
I boolean delete()
I boolean exists()
I String getAbsolutePath()
I String getName()
I String getParent()
I String getPath()
I boolean isAbsolute()
I boolean isDirectory()
I boolean isFile()
I boolean isHidden()
I long lastModified()
I long length()Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 16
![Page 17: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/17.jpg)
File class methods -Cont
I String[] list()
I File[] listFiles()
I boolean mkdir()
I boolean renameTo(File d)
I boolean setLastModifiedDate()
I boolean setReadOnly()
I String toString()
I String getFreeSpace()
I String getTotalSpace()
I String getUsableSpace()
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 17
![Page 18: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/18.jpg)
Programs using File class
I Program to rename a file.
I Program to delete a file.
I Creating a directory.
I Traversing a Directory.
I Using FilenameFilter Interface.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 18
![Page 19: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/19.jpg)
Programs using File class
I Program to rename a file.
I Program to delete a file.
I Creating a directory.
I Traversing a Directory.
I Using FilenameFilter Interface.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 19
![Page 20: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/20.jpg)
Programs using File class
I Program to rename a file.
I Program to delete a file.
I Creating a directory.
I Traversing a Directory.
I Using FilenameFilter Interface.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 20
![Page 21: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/21.jpg)
Programs using File class
I Program to rename a file.
I Program to delete a file.
I Creating a directory.
I Traversing a Directory.
I Using FilenameFilter Interface.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 21
![Page 22: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/22.jpg)
Programs using File class
I Program to rename a file.
I Program to delete a file.
I Creating a directory.
I Traversing a Directory.
I Using FilenameFilter Interface.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 22
![Page 23: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/23.jpg)
Program to rename a file.
import java.io.*;
import java.util.Scanner;
public class Rename
{public static void main(String args[]) {File f1, f2;
Scanner s = new Scanner(System.in);
System.out.println("Enter the file name : ");
String name old = s.nextLine();
System.out.println("Enter the new name : ");
String name new = s.nextLine();
f1 = new File(name old);
f2 = new File(name new);
if(f1.renameTo(f2))
System.out.println("File renamed");
else
System.out.println("Error Occured");
}}
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 23
![Page 24: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/24.jpg)
Program to delete a file.
import java.io.*;
import java.util.Scanner;
public class Rename
{public static void main(String args[]) {File f1;
Scanner s = new Scanner(System.in);
System.out.println("Enter the file name : ");
String name = s.nextLine();
f1 = new File(name);
if(f1.exists())
{if(f1.delete())
System.out.println("File deleted");
else
System.out.println("Error Occured");
}}}
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 24
![Page 25: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/25.jpg)
Types of Streams
I Byte Stream / Binary StreamI Only Binary data / bytes.I Read and Write [8 bytes].
I Character StreamI Character Data.I Unicode Character.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 25
![Page 26: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/26.jpg)
Types of Streams
I Byte Stream / Binary StreamI Only Binary data / bytes.I Read and Write [8 bytes].
I Character StreamI Character Data.I Unicode Character.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 26
![Page 27: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/27.jpg)
Stream Base IO
I Four Abstract Classes.I Byte Stream / Binary Stream
I InputStream.I OutputStream.
I Character StreamI Reader.I Writer.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 27
![Page 28: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/28.jpg)
Stream Base IO
I Four Abstract Classes.I Byte Stream / Binary Stream
I InputStream.I OutputStream.
I Character StreamI Reader.I Writer.
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 28
![Page 29: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/29.jpg)
Overview of IO Classes
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 29
![Page 30: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/30.jpg)
OutputStream of IO
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 30
![Page 31: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/31.jpg)
InputStream of IO
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 31
![Page 32: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/32.jpg)
Writer of IO
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 32
![Page 33: I/O in java Part 1](https://reader036.vdocument.in/reader036/viewer/2022062405/5558169ad8b42a5e468b4a96/html5/thumbnails/33.jpg)
Reader of IO
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 33