Download presentation
Presentation is loading. Please wait.
Published byClifton Carson Modified over 8 years ago
1
Recitation File I/O
2
File I/O File inFile = new File("data.txt"); File inFile = new File (“/Users/sunil/test.txt");
3
JFileChooser Selecting a File JFileChooser chooser = new JFileChooser(“directory to start from”); Chooser.showOpenDialog(null); File selectedFile = chooser.getSelectedFile(); File directory = chooser.getCurrentDirectory();
4
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
5
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();
6
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();
7
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();
8
Reading Data in Right Order Order of write and read operations must match in order to read the stored primitive data back correctly
9
Reading Data in Right Order Order of write and read operations must match in order to read the stored primitive data back correctly
10
Types of Files So far writes/reads have been on binary files To write to a text file, we use textfile input/output
11
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();
12
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);
13
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();
14
Textfile Input with Scanner Scanner scanner = new Scanner(new File("sample3.data")); int i = scanner.nextInt(); scanner.close()
15
Object File I/O The Object being written/read must implement serializable class Person implements Serializable {... }
16
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 )
17
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( );
18
Writing and Reading Arrays Person[] people = new Person[ N ]; outObjectStream.writeObject ( people ); Person[ ] people = (Person[]) inObjectStream.readObject ( )
19
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.