Download presentation
Presentation is loading. Please wait.
Published byAlaina Harvey Modified over 8 years ago
1
CSI 3125, Preliminaries, page 1 Files
2
CSI 3125, Preliminaries, page 2 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 FileInputStream and FileOutputStream
3
CSI 3125, Preliminaries, page 3 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 FileInputStream(String fileName) throws FileNotFoundException FileOutputStream(String fileName) throws FileNotFoundException fileName specifies the name of the file that you want to open
4
CSI 3125, Preliminaries, page 4 Reading and Writing Files When create an input stream, if the file does not exist, then FileNotFoundException is thrown. For output streams, if the file cannot be created, then FileNotFoundException is thrown.
5
CSI 3125, Preliminaries, page 5 Reading and Writing Files Closing the file close( ). It is defined by both FileInputStream and FileOutputStream, void close( ) throws IOException
6
CSI 3125, Preliminaries, page 6 Reading and Writing Files To read from a file, you can use a version of read( ) that is defined within FileInputStream. 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.
7
CSI 3125, Preliminaries, page 7 Reading and Writing Files import java.io.*; class f1 { public static void main(String a[]) { try{ FileInputStream fil=new FileInputStream("z.java "); // create object of FileInputStream 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");} }} Prog to read the file popo.java and print each characters in screen
8
CSI 3125, Preliminaries, page 8 Reading and Writing Files To write to a file, you will use the write( ) method defined by FileOutputStream void write(int byteval) throws IOException This method writes the byte specified by byteval to the file
9
CSI 3125, Preliminaries, page 9 Reading and Writing Files import java.io.*; class f1 { public static void main(String a[]) { try{ FileInputStream fin=new FileInputStream("z.java"); // object for file Input FileOutputStream fout=new FileOutputStream(“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");} } } Prog to copy the file popo.java to copy.java
10
CSI 3125, Preliminaries, page 10 Reading and Writing Files
11
CSI 3125, Preliminaries, page 11 Reading and Writing Files import java.io.*; class f { public static void main(String a[]){ try{ FileReader fil=new FileReader("popo.txt"); // FileReader reads text files BufferedReader br=new BufferedReader(fil); // Always wrap FileReader in BufferedReader. String s; while((s=br.readLine())!=null) //reference one line at a time { System.out.println(s); } br.close(); } catch(Exception e) {System.out.println("Error");} }} Prog to read the file popo.txt and print one line each in the screen
12
CSI 3125, Preliminaries, page 12 Reading and Writing Files
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.