Files CSI 3125 Preliminaries page 1 Reading and

  • Slides: 12
Download presentation
Files CSI 3125, Preliminaries, page 1

Files CSI 3125, Preliminaries, page 1

Reading and Writing Files • Java provides a number of classes and methods that

Reading and Writing Files • Java provides a number of classes and methods that allow you to read and write files. • In Java, all files are byte-oriented, and Java provides methods to read and write bytes from and to a file. • Java allows you to wrap a byte-oriented file stream within a character-based object. • The two important streams are File. Input. Stream and File. Output. Stream CSI 3125, Preliminaries, page 2

Reading and Writing Files • To open a file, create an object of one

Reading and Writing Files • To open a file, create an object of one of these classes, specifying the name of the file as an argument to the constructor. • While both classes support additional, overridden constructors • File. Input. Stream(String file. Name) throws File. Not. Found. Exception • File. Output. Stream(String file. Name) throws File. Not. Found. Exception • file. Name specifies the name of the file that you want to open CSI 3125, Preliminaries, page 3

Reading and Writing Files • When create an input stream, if the file does

Reading and Writing Files • When create an input stream, if the file does not exist, then File. Not. Found. Exception is thrown. • For output streams, if the file cannot be created, then File. Not. Found. Exception is thrown. CSI 3125, Preliminaries, page 4

Reading and Writing Files • Closing the file • close( ). It is defined

Reading and Writing Files • Closing the file • close( ). It is defined by both File. Input. Stream and File. Output. Stream, • void close( ) throws IOException CSI 3125, Preliminaries, page 5

Reading and Writing Files • To read from a file, you can use a

Reading and Writing Files • To read from a file, you can use a version of read( ) that is defined within File. Input. Stream. • int read( ) throws IOException • Each time it reads a single byte from the file and returns the byte as an integer value. • read( ) returns – 1 when the end of the file is encountered. • It can throw an IOException. CSI 3125, Preliminaries, page 6

Reading and Writing Files Prog to read the file popo. java and print each

Reading and Writing Files Prog to read the file popo. java and print each characters in screen • • • • • import java. io. *; class f 1 { public static void main(String a[]) { try{ File. Input. Stream fil=new File. Input. Stream("z. java"); // create object of File. Input. Stream int i; do { i = fil. read(); // read one character at a time System. out. println((char)i); // print in screen }while(i != -1); fil. close(); } catch(Exception e) {System. out. println("Error"); } }} CSI 3125, Preliminaries, page 7

Reading and Writing Files • To write to a file, you will use the

Reading and Writing Files • To write to a file, you will use the write( ) method defined by File. Output. Stream • void write(int byteval) throws IOException • This method writes the byte specified by byteval to the file CSI 3125, Preliminaries, page 8

Reading and Writing Files Prog to copy the file popo. java to copy. java

Reading and Writing Files Prog to copy the file popo. java to copy. java • • • • • • import java. io. *; class f 1 { public static void main(String a[]) { try{ File. Input. Stream fin=new File. Input. Stream("z. java"); // object for file Input File. Output. Stream fout=new File. Output. Stream(“copy. java"); // object for file output int i; do { i = fin. read(); fout. write(i); // write to the copy. java file }while(i != -1); fin. close(); fout. close(); } catch(Exception e) {System. out. println("Error"); } } } CSI 3125, Preliminaries, page 9

Reading and Writing Files CSI 3125, Preliminaries, page 10

Reading and Writing Files CSI 3125, Preliminaries, page 10

Reading and Writing Files Prog to read the file popo. txt and print one

Reading and Writing Files Prog to read the file popo. txt and print one line each in the screen • • • • • import java. io. *; class f { public static void main(String a[]) { try{ File. Reader fil=new File. Reader("popo. txt"); // File. Reader reads text files Buffered. Reader br=new Buffered. Reader(fil); // Always wrap File. Reader in Buffered. Reader. String s; while((s=br. read. Line())!=null) //reference one line at a time { System. out. println(s); } br. close(); } catch(Exception e) {System. out. println("Error"); } }} CSI 3125, Preliminaries, page 11

Reading and Writing Files CSI 3125, Preliminaries, page 12

Reading and Writing Files CSI 3125, Preliminaries, page 12