(c) Copyright 2000, 2001, 2002, 2003 by Daniel Berleant
![]()
![]()
Reading from a Local File
There
is a class FileInputStream
It has
methods associated with file input streams (surprise!)
. . . for
example, read(
)
We must declare
an object of class FileInputStream
(Objects
are like, unlike classes)
An object is an instance of a class
…like a variable is an instance of a type:
//myInt
is a variable of type int:
int myInt;
//f is
an object of class FileInputStream:
FileInputStream f;
To use a method in a class – must be declared static
To use a method in an object – must declare the object
Example:
FileInputStream myInput;
myInput is now an object for reading a
file
. .
. but which file?
. .
. we must provide myInput with memory to use, and say what file it
refers to:
myInput=
new FileInputStream("fileRead.dat");
I/O
can fail, so Java requires we account for that:
try{myInput=
new FileInputStream("fileRead.dat");}
...
If
it does fail, we must "catch" the failure:
try{myInput=
new FileInputStream("fileRead.dat");
}
catch
(IOException e)
{System.out.println(e);}
Let's analyze that code...
![]()
![]()
Sample codes
Let's see an example:
(If an example ever does not work, please send email)
import java.lang.*;import java.io.*; public class FileRead { static FileInputStream myInput; public static void main (String argv[ ]) { int Cbuffer=-1; char C; System.out.println("About to read a file..."); try {myInput = new FileInputStream( "FileRead.java"); } catch(IOException e){System.out.println(e);} do { try {Cbuffer=myInput.read();} catch (IOException e){ }; C=(char)Cbuffer; if (Cbuffer!=-1) System.out.print(C); } while (Cbuffer!=-1); System.out.println("...Finishing."); try {myInput.close();} catch(IOException e) {System.out.println(e);} } }
Let's analyze it .
. .
![]()
![]()
Writing to
a Local File
Here is
an example
import java.lang.*;import java.io.*; public class FileWrite { static FileOutputStream output; static DataOutputStream data; public static void main (String argv[ ]) {System.out.println ("About to write a file..."); try {output = new FileOutputStream("data.dat");} catch (IOException e) {System.out.println("Sorry...");} data=new DataOutputStream(output); try {data.writeBytes ("Hello there everyone!!"+"\n");} catch (IOException e){System.out.println(e);} System.out.println("...Finishing."); try {data.close();} catch(IOException e) { } try {output.close();} catch(IOException e) { } } }
Let's
analyze it
![]()
![]()
Files and URLs
Both must be converted to streams
Files: use e.g. the FileInputStream class
URLs: see code example below (also appears in a later session)
import java.net.*;import java.io.*;public class URLtest{private static URL location;public static void main(String argv[]) { try {location=new URL("http://www.iastate.edu");} catch(MalformedURLException e) {System.err.println ("Invalid URL: "+location);} InputStream myInput; BufferedReader dataInput; String lineOfText; try { myInput=location.openStream(); dataInput=new BufferedReader(new InputStreamReader(myInput)); System.out.println (dataInput.readLine()); while ((lineOfText=dataInput.readLine()) !=null) System.out.println(lineOfText); dataInput.close(); myInput.close(); } catch(IOException e) {System.out.println("oops!!!!!"); System.out.println ("Exception: "+e.toString() ); } }}