Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 18 File I/O CSE 1322 9/5/2019.

Similar presentations


Presentation on theme: "Lecture 18 File I/O CSE 1322 9/5/2019."— Presentation transcript:

1 Lecture 18 File I/O CSE 1322 9/5/2019

2 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 9/5/2019

3 Streams Streams hold data
Input streams provide data to the program (keyboard and ReadLine, System.in and Scanner) The program places data onto output streams (console and WriteLine, System.out and println) 9/5/2019

4 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 9/5/2019

5 File reading Must know the structure of the file
Is it one element per line or multiple elements per line or is it a combination of the two If 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. 9/5/2019

6 File Structure An airline company could store data in a file where each line represents a flight segment containing the following data: flight number origin airport destination airport number of passengers average ticket price  Such a file could contain the following data: AA123,BWI,SFO,235,239.5 AA200,BOS,JFK,150,89.3 In this case, the delimiter is a comma. 9/5/2019

7 Opening / Closing Files
Opening the file StreamReader sr = new StreamReader("data.txt"); StreamWriter sw = new StreamWriter("output.txt"); Closing the file sr.Close(); sw.Close(); 9/5/2019

8 Opening / Closing Files
Opening the input file Scanner file = new Scanner( new File( “filename.txt” ) ); or File datafile= new File("phoneList.txt"); Scanner in = new Scanner(datafile); Opening the output file FileOutputStream fos= new FileOutputStream ("phoneList.txt", false); PrintWriter pw= new PrintWriter(fos) Closing the file pw.close(); 9/5/2019

9 Methods of the PrintWriter Class
Return value Method name and argument list void Void print( int i ) print( double d ) print( String s ) println( int i ) println( double d ) println( String s ) writes the argument to the text file close( ) releases resources allocated to the PrintWriter object.

10 Java SOFTWARE ENGINEERING TIP
Calling the close method is optional. When the program finishes executing, all the resources of any unclosed files are released. It is good practice to call the close method, however, especially if you will be opening a number of files (or opening the same file multiple times.) Do not close the standard input, output, or error devices, however. They are intended to remain open. 9/5/2019

11 Reading / Writing data Reading data from the file
int x = Int32.Parse(sr.ReadLine()); or while (!sr.EndOfStream) { Console.WriteLine(sr.ReadLine()); } Writing data to the file sw.WriteLine(42); 9/5/2019

12 Reading / Writing data using Scanner in = new Scanner(datafile);
Reading data from the file String x = in.next(); int y = in.nextInt(); or while(in.hasNext()) { String line= in.nextLine(); String [] data= line.split(" "); } Writing data to the file pw.println(myData); 9/5/2019

13 loading a "game world" information:
private void LoadWorld() { StreamReader sr = new StreamReader("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]; } 9/5/2019

14 loading airline information
private void LoadData() { StreamReader sr= new StreamReader(“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(); 9/5/2019

15 loading airline information
private void LoadData() { 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(); 9/5/2019


Download ppt "Lecture 18 File I/O CSE 1322 9/5/2019."

Similar presentations


Ads by Google