Java I/O Part 1

33
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

description

File Handling in java

Transcript of Java I/O Part 1

Page 1: 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

Page 2: Java I/O Part 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

Page 3: Java I/O Part 1

Understanding Streams

Input Stream

Output Stream

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 3

Page 4: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

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: Java I/O Part 1

Overview of IO Classes

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 29

Page 30: Java I/O Part 1

OutputStream of IO

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 30

Page 31: Java I/O Part 1

InputStream of IO

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 31

Page 32: Java I/O Part 1

Writer of IO

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 32

Page 33: Java I/O Part 1

Reader of IO

Prof. AshishSingh Bhatia, [email protected], [email protected], Web: asbspace.in, M:9879009551 33