Java I/O Part 1
-
Upload
ashishsingh-bhatia -
Category
Education
-
view
530 -
download
3
description
Transcript of Java I/O Part 1
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
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
Understanding Streams
Input Stream
Output Stream
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 3
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Overview of IO Classes
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 29
OutputStream of IO
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 30
InputStream of IO
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 31
Writer of IO
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 32
Reader of IO
Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 33