CSCI 143 File IO - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

CSCI 143 File IO

Description:

int read() throws IOException //reads a single character ... public FileWriter (String fileName) throws IOException ... throws IOException, EOFException ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 19
Provided by: william591
Category:

less

Transcript and Presenter's Notes

Title: CSCI 143 File IO


1
CSCI 143File I/O
2
Types of Data
  • character data
  • represented in 2-byte Unicode within Java
  • represented as 1-byte ASCII in most files in US
  • read with Reader subclasses
  • written with Writer subclasses
  • binary data
  • read with InputStream subclasses
  • written with OutputStream subclasses
  • java.io package class hierarchy

3
java.io package
  • BufferedInputStream BufferedOutputStream
    BufferedReader BufferedWriter CharArrayReader
    CharArrayWriter DataInputStream
    DataOutputStream File FileDescriptor
    FileInputStream FileOutputStream
    FilePermission FileReader FileWriter
    FilterInputStream FilterOutputStream
    FilterReader FilterWriter InputStream
  • InputStreamReader
  • ObjectInputStream ObjectInputStream.GetField
    ObjectOutputStream ObjectOutputStream.PutField
    ObjectStreamClass ObjectStreamField
    OutputStream OutputStreamWriter PrintStream
    PrintWriter RandomAccessFile Reader
    SequenceInputStream SerializablePermission
    StreamTokenizer StringBufferInputStream
    StringReader StringWriter Writer

4
Reading Character Data from a File
  • class FileReader
  • extends InputStreamReader which extends Reader
  • constructor
  • public FileReader (String fileName) throws
    FileNotFoundException
  • important methods
  • int read() throws IOException //reads a single
    character
  • int read(char buf, int start, int len) throws
    IOException

  • // reads len characters
  • returns -1 for EOF

5
Reading Character Data
  • BufferedReader extends Reader
  • fills a buffer at a time for much greater
    efficiency
  • constructor
  • public BufferedReader (Reader reader)
  • important methods
  • String readLine() throws IOException //reads a
    single line
  • returns null for EOF
  • common usage (ignoring exceptions)
  • BufferedReader rdr new BufferedReader (new
    FileReader (Grades.txt))
  • String line
  • while ((linerdr.readLine()) ! null) //
    process line

6
Writing Character Data to a File
  • class FileWriter
  • extends OutputStreamWriter which extends Writer
  • constructors
  • public FileWriter (String fileName) throws
    IOException
  • public FileWriter (String fileName, boolean
    append)throws IOException
  • important methods
  • void write(char c) throws IOException // writes
    a single char
  • void write(char buf, int start, int len)
    throws IOException
  • void write(String str, int start, int len)
    throws IOException
  • void flush() throws IOException // forces
    output
  • void close() throws IOException // flushes

7
Writing Character Data
  • BufferedWriter extends Writer
  • writes a buffer at a time for much greater
    efficiency
  • constructor
  • public BufferedWriter (Writer writer)
  • interesting methods
  • void newLine() throws IOException //outputs a
    newline
  • common usage (ignoring exceptions)
  • BufferedWriter wrtr new BufferedWriter (new
    FileWriter(Grades.txt, true))
  • String s Smith 2.0
  • wrtr.write (s, 0, s.length())
  • wrtr.newLine()
  • wrtr.close()

8
Writing Character Data
  • PrintWriter extends Writer
  • converts data to strings for output
  • throws no exceptions
  • constructors
  • public PrintWriter (Writer writer)
  • public PrintWriter (Writer writer, boolean
    autoflush)
  • interesting methods
  • void print(boolean b)//outputs a boolean as a
    string
  • void print(int i) //outputs an int as a string
  • void print(double d) //outputs a double as a
    string
  • void print(String s) //outputs a String as a
    string
  • void print(Object o) //outputs an Object as a
    string

9
Writing Character Data
  • PrintWriter (continued)
  • common usage
  • PrintWriter pwrtr new PrintWriter (new
    BufferedWriter (new FileWriter (Grades.txt,
    true)))
  • String name Smith
  • double grade 2.0
  • pwrtr.print (name )
  • pwrtr.println (grade)
  • pwrtr.close()

10
Reading Binary Data from a File
  • class FileInputStream extends InputStream
  • constructor
  • public FileInputStream (String fileName) throws
    FileNotFoundException
  • interesting methods
  • int read() throws IOException //reads a single
    byte
  • int read(byte buf, int start, int len) throws
    IOException // reads len bytes
  • void close() throws IOException //closes the
    file
  • returns -1 for EOF

11
Reading Binary Data
  • BufferedInputStream extends InputStream
  • fills a buffer at a time for much greater
    efficiency
  • constructor
  • public BufferedInputStream (InputStream stream)
  • common usage (ignoring exceptions)
  • BufferedInputStream bis
  • new BufferedInputStream (
  • new FileInputStream (Grades.dat))

12
Reading Binary Data
  • DataInputStream extends FilterInputStream
  • reads primitive data types
  • constructor
  • public DataInputStream (InputStream stream)
  • interesting methods
  • char readChar() throws IOException, EOFException
  • int readInt() throws IOException, EOFException
  • float readFloat() throws IOException,
    EOFException
  • double readDouble() throws IOException,
    EOFException

13
  • DataInputStream dis null
  • try
  • //open stream
  • dis new DataInputStream (
  • new BufferedInputStream (
  • new FileInputStream (Grades.dat)))
  • catch (FileNotFoundException e)
  • System.exit(1)
  • try
  • // read data
  • while (true)
  • double d dis.readDouble()
  • System.out.println (d)

14
Writing Binary Data to a File
  • class FileOutputStream extends OutputStream
  • constructors
  • public FileOutputStream (String fileName) throws
    IOException
  • public FileOutputStream (String fileName,
    boolean append) throws IOException
  • interesting methods
  • void write(int i) throws IOException // writes
    an int
  • void write(byte buf) throws IOException
  • void write(byte buf, int start, int len) throws
    IOException
  • void flush() throws IOException // forces
    output
  • void close() throws IOException // flushes

15
Writing Binary Data
  • BufferedOutputStream extends OutputStream
  • writes a buffer at a time for much greater
    efficiency
  • constructor
  • public BufferedOutputStream (OutputStream
    writer)
  • common usage (ignoring exceptions)
  • BufferedOutputStream bos
  • new BufferedOutputStream (
  • new FileOutputStream (Grades.dat,
  • true))

16
Writing Binary Data
  • DataOutputStream extends FilterOutputStream
  • writes primitive data types
  • constructor
  • public DataOutputStream (OutputStream stream)
  • interesting additional methods
  • void writeChar(char c) throws IOException
  • void writeInt(int i) throws IOException
  • void writeFloat(float f) throws IOException
  • void writeDouble(double d) throws IOException

17
Interrogating a File
  • class File
  • represents a file or folder (directory)
  • constructor
  • public File (String path)
  • public File (String path, String name)

18
Interrogating a File
  • Interesting methods
  • boolean exists()
  • boolean isDirectory()
  • boolean isFile()
  • boolean canRead()
  • boolean canWrite()
  • long length()
  • long lastModified()
  • boolean delete()
  • boolean renameTo(File dest)
  • boolean mkdir()
  • String list()
  • File listFiles()
  • String getName()
Write a Comment
User Comments (0)
About PowerShow.com