Lecture 18
File I/O
CSE 1322
6/26/2019
18-1
File Input / Output
Consider that a program may want to make its data persistent (or not rely upon the user for input)Reading from and writing to a file is conceptually the same as reading/writing to the console
18-2
6/26/2019
Streams
Streams hold dataInput streams provide data to the program (keyboard andReadLine, System.in and Scanner)The program places data onto output streams (console and WriteLine,System.outandprintln)
18-3
6/26/2019
Process:
Open the file stream(many "modes" - read, write, append, create, etc.)Do what you need to do (read/write)Close the file stream(freeing it for use by other programs)You can imagine that there is an "active cursor" that is moved around the file as you read/write
18-4
6/26/2019
File reading
Must know the structure of the fileIs it one element per line or multiple elements per line or is it a combination of the twoIf it is multiple elements, how are the fields divided?Is there a delimiter such as a space or ;Are the fields off defined size such as first name in the first 10, last name in the next 20, age in the next 3, etc.
6/26/2019
18-5
File Structure
An airline company could store data in a file where each line represents a flight segment containing the following data:flight numberorigin airportdestination airportnumber of passengersaverage ticket priceSuch a file could contain the following data:AA123,BWI,SFO,235,239.5AA200,BOS,JFK,150,89.3Inthis case, the delimiter is a comma.
6/26/2019
18-6
Opening / Closing Files
18-7
6/26/2019
Opening / Closing Files
Opening the input fileScannerfile = new Scanner( new File( “filename.txt” ) );orFiledatafile= new File("phoneList.txt");Scannerin = new Scanner(datafile);Opening the output fileFileOutputStreamfos= newFileOutputStream("phoneList.txt", false);PrintWriterpw= newPrintWriter(fos)Closing the filepw.close();
18-8
6/26/2019
Methods of thePrintWriterClass
Calling theclosemethod is optional. When the program finishes executing, all the resources of any unclosed files are released.It is good practice to call theclosemethod, however, especially if you will be opening a number of files (or opening the same file multiple times.)Do not close the standardinput, output, or error devices, however. They are intended to remain open.
Java SOFTWARE ENGINEERINGTIP
6/26/2019
18-10
Reading / Writing data
Reading data from the fileintx = Int32.Parse(sr.ReadLine());orwhile (!sr.EndOfStream){Console.WriteLine(sr.ReadLine());}Writing data to the filesw.WriteLine(42);
18-11
6/26/2019
Reading / Writing data
using Scannerin = new Scanner(datafile);Reading data from the fileString x =in.next();int y =in.nextInt();orwhile(in.hasNext()){String line=in.nextLine();String [] data=line.split("");. . . }Writing data to the filepw.println(myData);
18-12
6/26/2019
loading a "game world" information:
private voidLoadWorld(){StreamReadersr= newStreamReader("world.txt");world_width= Int32.Parse(sr.ReadLine());world_height= Int32.Parse(sr.ReadLine());show_width= Int32.Parse(sr.ReadLine());show_height= Int32.Parse(sr.ReadLine());world_background= new int[world_width,world_height];}
18-713
6/26/2019
loading airline information
private voidLoadData(){StreamReadersr= newStreamReader(“data.txt”);char[]delims= { ',' };string info =sr.ReadLine();while( info!= null){string [] tokens =info.Split(delims);//create/assign appropriate objects, etc.info=sr.ReadLine();}sr.Close();}
18-14
6/26/2019
loading airline information
private voidLoadData(){Scanner read=new Scanner(new File(“data.txt”));char[]delims= { ',' };while(read.hasNext()){String info=read.nextLine();String [] tokens =info.split(delims);//create/assign appropriate objects, etc.}read.close();}
18-15
6/26/2019
0
Embed
Upload