Files and Streams

Before you can read from a file, you must open it. After you are done reading from a file, you must close it. There are two common varieties of reading
reading characters ( a Character is 16 bites long )
reading bytes (a byte is 8 bits long)

Inevitably, when you sit down to read from a file, you have a sort through the choices on those lists. The best approach is to pick one from either list…….
Character and byte—and learn to use it .

7.1 Reading Characters from a File
Say you want to read from a file.
• You will need to open the file.
• You will need to read from the file to the end.
• You will need to close the file.
First of all, what is a file? A file is an instance of the class File import Java.io;
public class FileRead
{
public FileRead ( )
{
File inFile = new File ( "C : /orig/aFile.txt") ;
}
public static void main (String [ ] args )
{
FileRead fr = new FileRead ( ) ;
}
}
All the classes used inI/ ocome from this package. Because the constructor on File throws an IOException, we are forced to place it in a try-catch block:
import java.io;
public class FileRead
{
public FileRead ( )
{
try
{
File inFile = new File ( "C : /orig/aFile.txt") ;
}
catch (IoException io)
{
System.out.println ("IOException, io=" + io) ;
}
}
public static void main (String [ ] args )
{
FileRead fr = new FileRead ( ) ;
}
Now an example to read characters from a file, and store it as integers.
public class FileRead
{
public FileRead ( )
{
try
{
File inFile = new File ( "C : /orig/aFile.txt") ;
File outFile = new File ("C : / final/outFile.txt") ;
FileReader fr = new FileReader (inFile) ;
FileWriter fw = new Filewriter (outFile) ;
int capital = 0;
boolean keepReading = true;
While (keepReading)
{
c = fr.read ( ) ;
if ( c ==-1 )'
{
keepReading = false;
}
else
{
fw.write ( c ) ;
}
}
fr.close ( ) ;
fw. close ( ) ;
}
}
}


7.2 Reading bytes from a file
The approach for reading bytes is nearly the same. The difference comes in the classes we choose to do the reading and writing.
public class FileRead
public class FileRead
{
public FileRead ( )
{
try
{
File inFile = new File ( "C : /orig/aFile.txt") ;
File outFile = new File ("C : / final/outFile.txt") ;
FileInputStream fir = new FileInputStream (inFile) ;
FileoutputStream fos = new FileoutputStream (outFile) ;
int capital = 0;
Boolean keepReading = true;
While (keepReading)
{
c = fr.read ( ) ;
if ( c ==-1 )'
{
keepReading = false;
}
else
{
fw.write ( c ) ;
}
}
fr.close ( ) ;
fw. close ( ) ;
}
}
}


7.3 Alternatives for efficiency
As you can imagine, reading a byte or a character at a time is pretty inefficient. For that reason, there are alternatives. The best one is the BufferedReader. This class gathers a chunk of data at a road. The BufferedWriter also allows us to write an entire String.
public class FileRead
{
public FileRead ( )
{
try
{
File inFile = new File ( "C : /orig/aFile.txt") ;
File outFile = new File ("C : / final/outFile.txt") ;
FileReader fr = new FileReader (inFile) ;
BufferedReader br = new BufferedReader (fr) ;
FileWriter fw = new Filewriter (outFile) ;
BufferedWriter bw = new BufferedWriter (fw) ;
String temp = null;
boolean keepReading = true;
While (keepReading)
{
temp = br.readLine ( ) ;
if (temp = = null)
{
keepReading = false;
}
else
{
bw.write ( temp ) ;
}
}
br.close ( ) ;
fr.close ( ) ;
bw.close ( ) ;
fw. close ( ) ;
}
}
}

7.4 File Serialization
Another variant of the file I/O world is something called serialization. To serialize an object means to take an object in memory and write that object to a file. Then, at a later time, the object can be-serialized and then we have the object—with its state intact—back in memory.
String myString = new String ("Some important text") ;
File myFile = new File ( "C :/myFile.ser") :
FileOutputStream s = new FileOutputStream (myFile) ;
objectOutputStream s = new objectOutputStream (out) ;
s.writeObject (myString) ;
s.flush ( ) ;
To start, we create an object of type String "myString". Next, notice we are using a special class called an ObjectOutputStream. This class is designed to serialize objects. By custom, files of serialized objects end in "ser" Finally. we see that we are writing an object.
The process to read from an existing Serialized file is very similar.
File myFile = new File ( "C :/myFile.ser") :
FileInputStream s = new FileInputStream (myFile) ;
objectInputStream s = new objectInputStream (In) ;
String myString = (String) s.readobject ( ) ;
Notice, when you read out the object from serialized file, you need to cast the object back into the type you know it is.

7.5 Reading user input from the console
Although it should be easy, you have to consider the user inputting values from the console as reading from a stream. Before we look at the program, let's understand the issues involved:
• Now do we tell it to read?
• Now do we tell it to stop reading?
you tell it to read a line by hitting the "return" key on your keyboard. To tell it when to stop reading, we need to send in a "sentinel" value. That means, no matter what, stop when you read this "sentinel" value.
String temp = null'
Boolean keepReading = true;
InputStream is = System.in;
InputStreamReader isr = new InputStreamReader (is) ;
BufferedReader br = new BufferedReader (isr) ;

StringBuffer stuffRead = new StringBuffer ( ) ;
try
{
While (keepReading)
{
temp = br.readLine ( ) ;
if (temp == null . . temp.length ( ) == 0 )
{
keepReading = false;
}
else
{
stuffRead.append (temp) ;
}
}
System.out.println ("stuffRead=" + stuffRead.toString ( ) ; )
}
catch ( IOException io )
{
System.out.printin ("ConsoleReader Constructor threw an IOException, io=" + io) ;
}
Here, we see that an entry of spaces is the sentinel value