PPT Slides

advertisement
Recitation
File I/O
File I/O
• File inFile = new File("data.txt");
• File inFile = new File (“/Users/sunil/test.txt");
JFileChooser
• Selecting a File
• JFileChooser chooser = new
JFileChooser(“directory to start from”);
• Chooser.showOpenDialog(null);
• File selectedFile = chooser.getSelectedFile();
• File directory = chooser.getCurrentDirectory();
File methods
• inFile.exists() – see if inFile is a real file
• inFile.isFile() – check to see if inFile is a file or
directory
• Directory.list() – returns a string[] of files
inside the directory
Streams for Low Level File I/O
• FileOutputStream and FileInputStream
• Writes only bytes to an outfile, or read bytes from an
outfile
File file = new File(“sample1.data”)
FileOutputStream outStream = new
FileOutputStream(file);
outStream.close();
OR
FileInputStream inStream = new FileInputStream(file);
inStream.close();
Streams for High-Level File I/O
File outFile = new File( "sample2.data" );
FileOutputStream outFileStream = new FileOutputStream(outFile);
DataOutputStream outDataStream = new DataOutputSteam
(outFileStream);
outDataStream.writeInt(2343);
outDataStream.writeLong(34545L);
outDataStream.writeFloat(23.34F);
outDataStream.writeDouble(456.4234D);
outDataStream.writeChar('J');
outDataStream.writeBoolean(true)
outDataStream.close();
outFileStream.close();
Streams for High-Level File I/O
File inFile = new File( "sample2.data" );
FileInputStream inFileStream = new FileInputStream(inFile);
DataInputStream inDataStream = new
DataInputSteam(inFileStream);
int i = inDataStream.readInt();
long l = inDataStream.readLong();
float f = inDataStream.readFloat();
double d = inDataStream.readDouble();
char c = inDataStream.readChar();
boolean bool =
inDataStream.readBoolean();inDataStream.close();
inFileStream.close();
Reading Data in Right Order
• Order of write and read operations must
match in order to read the stored primitive
data back correctly
Reading Data in Right Order
• Order of write and read operations must
match in order to read the stored primitive
data back correctly
Types of Files
• So far writes/reads have been on binary files
• To write to a text file, we use textfile
input/output
Text file output
File outFile = new File("sample3.data");
FileOutputStream outFileStream = new
FileOutputStream(outFile);
PrintWriter outStream = new
PrintWriter(outFileStream);
outStream.println(2343);
outStream.println("Hello, world.");
outStream.println(true);
outStream.close();
outFileStream.close();
Text file output
• To append to a file instead of writing over the
file, create the outFileStream as follows:
FileOutputStream outFileStream = new
FileOutputStream(outFile, true);
Text file input
File inFile = new File("sample3.data");
FileReader fileReader = new FileReader(inFile);
BufferedReader bufReader = new
BufferedReader(fileReader);
String str = bufReader.readLine();
int i = Integer.parseInt(str);
bufReader.close();
fileReader.close();
Textfile Input with Scanner
Scanner scanner = new Scanner(new
File("sample3.data"));
int i = scanner.nextInt();
scanner.close()
Object File I/O
• The Object being written/read must
implement serializable
class Person implements Serializable {
...
}
Writing Objects
File outFile = new File("objects.data");
FileOutputStream outFileStream = new
FileOutputStream(outFile);
ObjectOutputStream outObjectStream = new
ObjectOutputStream (outFileStream);
account1 = new Account();
bank1 = new Bank();
outObjectStream.writeObject( account1 );
outObjectStream.writeObject( bank1 )
Reading Objects
File inFile = new File("objects.data");
FileInputStream inFileStream = new
FileInputStream(inFile);
ObjectInputStream inObjectStream = new
ObjectInputStream(inFileStream);
Account account1 = (Account)
inObjectStream.readObject( );
Bank bank1 = (Bank)
inObjectStream.readObject( );
Writing and Reading Arrays
Person[] people = new Person[ N ];
outObjectStream.writeObject ( people );
Person[ ] people = (Person[])
inObjectStream.readObject ( )
When to stop reading
• Different reader classes return different
signals at the end of file
• Binary file readers throw the EOFException at
the end of file
• Text file readers return null or -1
Download