Handling Files with Java - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Handling Files with Java

Description:

... are usually responsible of keeping track and managing all the files on a system ... Method nextToken returns an integer of one of the following types ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 30
Provided by: bas44
Category:

less

Transcript and Presenter's Notes

Title: Handling Files with Java


1
Handling Files with Java
  • java.io

2
File
  • File is a collection of data stored on disk that
    has a specific name
  • Operating systems are usually responsible of
    keeping track and managing all the files on a
    system
  • Java as programming language, provides constructs
    to manipulate files

3
Java File Manipulation
  • java.io.File is the class used for accessing
    files on your disk
  • File object does not represent a file
  • Rather it represents a pathname or reference
  • This path may or may not exist

4
Creating File Object
  • File f new File ( C\\temp\\new.txt)
  • f represents a file named C\temp\new.txt
  • File f2 new File(C\\temp)
  • Here f2 represents not a file, but the directory
    C\temp
  • Note File object may refer to a path or file
    that does not exist

5
Different ways of creating File object
  • File f new File(C\\temp\\new.txt)
  • File f new File(C/temp/new.txt)
  • File f new File(C/temp, new.txt)

6
Relative Path
  • File f new File(something.doc)
  • Something.doc is placed in the folder where your
    program (class files) are placed

7
Getting information about File
  • getName( ) method returns the file name , without
    Path
  • File f new File(C\\temp\\no.txt)
  • String s f.getName( )
  • Here s has no.txt

8
Getting information about File
  • getPath( ) method returns the Path including the
    file name
  • File f new File(C\\temp\\no.txt)
  • String s f.getName( )
  • Here s has C\\temp\\no.txt

9
Getting information about File
  • getParent( ) method returns the string containing
    path of parent directory
  • File f new File(C\\temp\\child)
  • String s f.getParent( )
  • Here s has C\\temp

10
Getting information about File
  • exists( ) checks whether the specified path or
    file exists or not
  • File f new File(C\\temp\\test.doc)
  • Here f.exists( ) returns true if above file
    exists, otherwise returns false.

11
Getting information about File
  • isDirectory( )
  • isFile( )
  • canRead( )
  • canWrite( )
  • list( )
  • listFiles( )

12
Creating Directories
  • File f new File (C\newDir)
  • f.mkdir()

13
Creating Empty File
  • File f new File( C\newFile.txt)
  • if( ! f.exists( ) )
  • f.createNewFile( )

14
Input and Output
  • Java handles I/O using a set of classes found in
    java.io.
  • Input of all types is handled through InputStream
  • Output of all types is handled through
    OutputStream

15
InputStream and OutputStream
  • A stream is a sequential flow of bytes
  • Input Stream is a flow of data from some external
    source into our program
  • Output stream is a flow of data from out program
    to some external target

16
Reading data from InputStream
  • We instantiate an InputStream object
  • Associate it with some source of data
  • Read data in some memory location

17
Reading data from File
  • We use a special type of Input Stream for reading
    data from file named FileInputStream
  • FileInputStream is the sub-class of InputStream

18
Reading data from file
  • // Creating a file object
  • File f new File("doc.txt")
  • //Creating FileInputStream object and associating
    it to the above file
  • FileInputStream in new FileInputStream (f)

19
Reading data from file
  • byte b new byte20
  • for(int i0 iltb.length i)
  • bi(byte)in.read()
  • String s new String (b)
  • System.out.println( s)
  • This program prints first 20 characters from the
    file into the byte array

20
Detecting End of File
  • If read( ) method returns -1, this means that the
    stream has ended i.e. we have reached the end of
    file

21
Another way of reading data from file
  • If you know the total number of bytes you need to
    read from file, you can use this code
  • File f new File("doc.txt")
  • FileInputStream in new FileInputStream (f)
  • byte b new byte20
  • in.read(b)
  • Sytem.out.println( new String(b))

22
Reading strings and numbers
  • Above program reads a specified number of data
  • We now write a program that reads a program TOKEN
    by TOKEN
  • Prints every token on a new line
  • We need a class called StreamTokenizer for this
    purpose

23
Reading Tokens from file
  • File f new File("doc.txt")
  • StreamTokenizer st new StreamTokenizer( new
    BufferedReader( new FileReader(f)))
  • int tokenType
  • while ( true )
  • tokenType st.nextToken()
  • if(tokenType st.TT_EOF)
  • break
  • if(tokenType st.TT_NUMBER)
  • System.out.println(st.nval)
  • if(tokenType st.TT_WORD)
  • System.out.println(st.sval)

24
How it works
  • StreamTokenizer st new StreamTokenizer( new
    BufferedReader( new FileReader(f)))
  • File reader is a utility class used for reading
    character files
  • Buffered Reader buffers the data read from the
    stream till the buffer is full
  • StreamTokenizer is used to read tokens out of
    buffered reader

25
StreamTokenizertokenType st.nextToken()
  • Method nextToken returns an integer of one of the
    following types
  • nval If the current token is a number, this
    field contains the value of that number. 
  • Val If the current token is a word token, this
    field contains a that string
  • TT_EOF A constant indicating that the end of the
    stream has been read.
  • TT_EOL A constant indicating that the end of the
    line has been read
  • TT_NUMBER A constant indicating that a number
    token has been read.
  • TT_WORD A constant indicating that a word token
    has been read. 
  • Ttype   After a call to the nextToken method,
    this field contains the type of the token just
    read

26
Reading Tokens from file
  • File f new File("doc.txt")
  • StreamTokenizer st new StreamTokenizer( new
    BufferedReader( new FileReader(f)))
  • int tokenType
  • while ( true )
  • tokenType st.nextToken()
  • if(tokenType st.TT_EOF)
  • break
  • if(tokenType st.TT_NUMBER)
  • System.out.println(st.nval)
  • if(tokenType st.TT_WORD)
  • System.out.println(st.sval)

27
Writing Data to Streams
  • Create an OutputStream
  • Associate it with a data destination
  • Write data to it

28
Writing Data to File
  • File f new File("docz.txt")
  • FileOutputStream of new FileOutputStream(f)
  • String s " This data is to be written to the
    file "
  • of.write(s.getBytes())
  • of.close()

29
Further Reading
  • Chapter 8, 9, 10, 11 of Ivor-Horton
  • Lets try things out now..
Write a Comment
User Comments (0)
About PowerShow.com