Java OOp I - O Straeming
Java OOp I - O Straeming
FILE MANAGEMENT
INPUT/OUTPUT BASICS
I/O Streaming
Why I/O Streaming?
Data stored in variables and arrays is temporary- it’s lost when a local variable goes out of scope or when
the program terminates.
For long-term retention of data, even after the programs that create the data terminate, computers use
files.
You can write data to a file instead of the computer screen.
You can write certain data to a file while still putting other data on the screen or you may want to query
the user for input rather than accepting it all on the command line.
Or maybe you want to read data out of a file that's in a particular format.
In Java all these methods take place as streams using File I/O streams.
The System.out.println() statement we've been using all along is an implementation of Streams.
In order to use the Java file classes, we must import the Java Input/Output package (java.io) in the following
manner import java.io.*;
I/O Streaming
Reading and Writing with Java's Input / Output Streams
Input/Output = I/O = communication between a computer program and external sources and
destinations of information
Involves reading and writing
Reading input from a source
Writing output to a destination
Example sources and destinations are: Files, Network connections, Other programs
Java uses an I/O system called streams (pioneered in C++)
Java provides java.io package to implement streams
Streams treat all external source and destinations of data the same way: as "streams" of information
Input Vs. Output Streams
if (br != null) {
try { br.close(); }
catch (IOException e) { /* ignore */ }
}
return line;
}
I/O Streaming
Example : Write the Fahrenheit to Celsius table in a file
while (fahr <= upper) {
import java.io.*; // while loop begins here
class FahrToCelsius { celsius = 5.0 * (fahr-32.0) / 9.0;
public static void main (String args[]) { myOutput.println(fahr + " " + celsius);
double fahr, celsius; fahr = fahr + step;
double lower, upper, step;
} // while loop ends here
lower = 0.0; // lower limit of temperature table
} // try ends here
upper = 300.0; // upper limit of temperature table
catch (IOException e) {
step = 20.0; // step size System.out.println("Error: " + e);
fahr = lower; }
try { } // main ends here
FileOutputStream fout = new }//End Class
FileOutputStream("c:\\test.out");
} // end while
} // end try
I/O Streaming
catch ( NoSuchElementException elementException )
{ System.err.println( "File improperly formed." );
input.close();
} // end catch 1
catch ( IllegalStateException stateException )
{
System.err.println( "Error reading from file." );
System.exit( 1 );
} // end catch 2
} // end method readRecords
I/O Streaming
public void closeFile() {// close file and terminate application
if ( input != null )
input.close(); // close file
} // end method closeFile
} // end class ReadingFromFile
public class TestIO {
public static void main(String[] args) {
ReadingFromFile read = new ReadingFromFile ();
read.openFile ();
read.readRecords ();
read.closeFile();
}}
I/O Streaming
The End