Java File IO - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Java File IO

Description:

We will use our friend the BufferedReader Class in conjunction with a class ... of using BufferedReader NOTE: if file can't be opened IOException is thrown! ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 14
Provided by: nickga9
Category:
Tags: file | ioexception | java

less

Transcript and Presenter's Notes

Title: Java File IO


1
Java File I/O
  • N Gattuso 12/2003

2
Reading Info (Strings) From a File
  • We will use our friend the BufferedReader Class
    in conjunction with a class called FileReader
  • So as usual first we need to import the java.io.
    package.
  • And we need to define a String .. Lets call it
    record String recordnull

3
Reading Info (Strings) From a File
  • Next we need to create an object from the
    FileReader class
  • FileReader fr new FileReader("C\\mydata.txt")
  • Above code creates a FileReader Object called fr
    this is just a pointer to the file!

4
Reading Info (Strings) From a File
  • Now that the file is open we need to read
    contents of using BufferedReader NOTE if file
    cant be opened IOException is thrown!
  • BufferedReader br new BufferedReader(fr)

5
Reading Info (Strings) From a File
  • Every line in the file is considered a string
    so we use a while loop to read all lines in file
    when file empty we will receive a null passed
    back into our string

6
Reading Info (Strings) From a File
  • while ( (recordbr.readLine()) ! null )
    System.out.println(record)

7
Reading Info (Strings) From a File
  • while ( (recordbr.readLine()) ! null ) //
    read all lines from br which// is a
    BufferedReader Object pointing // to the fr
    FileReader object// line by line in file goes
    into record which // is a string when file
    empty, record // string is set to key word null
    (empty)

8
Reading Info (Strings) From a File
  • When done reading, close the BufferedReader
    object -gt br.close()
  • Also, good practice to close the FileReader
    object as well -gt fr.close()
  • Note when opening the file we passed
  • C\\mydata.txt \ is a special character
    which needs to be escaped with another \ to take
    special meaning away.

9
Writing Info to a File
  • We need to create an object from the FileWriter
    class
  • FileWriter fw new FileWriter ("C\\new.txt,true
    ) -gt Note 2 args, first is file name, 2nd is
    boolean true to append!
  • Above code creates a FileWriter Object called fw
    this is just a pointer to the file!

10
Writing Info to a File
  • Now that the file is open we need to create a
    write output buffer to using BufferedWriter
    NOTE if file cant be opened IOException is
    thrown!
  • BufferedWriter wr new BufferedWriter(fw)

11
Writing Info to a File
  • To Write a line (String) of info to an open file
    using BufferedWriter
  • wr.write(record,0,record.length())
  • // where record is a string (first arg) with
    info to
  • // write to file
  • // 0 is the offset to start writing info in
    string // from and record.length() is where to
    // stop in record string so all info in
  • // record string is written

12
Writing Info to a File
  • When done writing, close the BufferedWriter
    object -gt wr.close()
  • Also, good practice to close the FileWriter
    object as well -gt fw.close()

13
Putting it all together
  • File.java reads every line from a file called
    C\mydata.txt and writes to a file called
    C\new.txt -gt so it is the copy command found in
    DOS if you add the args string logic via main
    you can pass command line args make full copy
    command..
  • Demo file.java via source code run!
Write a Comment
User Comments (0)
About PowerShow.com