Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.

Slides:



Advertisements
Similar presentations
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
Advertisements

Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
1 Various Methods of Populating Arrays Randomly generated integers.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
The Print Formatting Statement … named printf. 2 Introduction to printf statements print and println statements don’t allow us to easily format output.
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Introduction to Computer Programming Looping Around Loops I: Counting Loops.
Introduction to Computers and Programming Lecture 3: Variables and Input Professor: Evan Korth New York University.
Introduction to Computer Programming Loops N Decisions If/Else Counting Loops.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Chapter 17: Arrays Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
PHP meets MySQL.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Intro and Review Welcome to Java. Introduction Java application programming Use tools from the JDK to compile and run programs. Videos at
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Winter 2006CISC121 - Prof. McLeod1 Last Time Misc. useful classes in Java: –String –StringTokenizer –Math –System.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Algorithms  Problem: Write pseudocode for a program that keeps asking the user to input integers until the user enters zero, and then determines and outputs.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
CMSC 202 Text File I/O. Aug 8, Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Files Tutor: You will need ….
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
A First Program CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington Credits: a significant part of.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Strings CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
COMP 110: Spring Announcements Program 5 Milestone 1 was due today Program 4 has been graded.
Introduction to Arrays. Learning Objectives By the end of this lecture, you should be able to: – Understand what an array is – Know how to create an array.
File Input & Output Sections Outcomes  Know the difference between files and streams  Use a Scanner to read from a file  add “throws” annotations.
Formatted Output (printf) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
Common Mistakes with Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Strings CSE 1310 – Introduction to Computers and Programming
Strings CSE 1310 – Introduction to Computers and Programming
Strings CSE 1310 – Introduction to Computers and Programming
Whatcha doin'? Aims: To start using Python. To understand loops.
File - CIS 1068 Program Design and Abstraction
CMSC 202 Text File I/O.
Formatted Output (printf)
Loops BIS1523 – Lecture 10.
How to Run a Java Program
Variables ICS2O.
Fundamentals of Data Structures
How to Run a Java Program
File Handling in Java January 19
Arrays CSE 1310 – Introduction to Computers and Programming
Files & Exception Handling
Strings CSE 1310 – Introduction to Computers and Programming
Input and Formatted Output (printf)
How to Run a Java Program
Presentation transcript:

Files CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1

Files and Text Files A file is a collection of data, that is saved on a hard drive. A text file contains text (as opposed to images, video, songs, etc.) Every file is uniquely identified using two attributes: – The file name. – The file path. 2

What You Should Know You are expected to know: – How to create a text file. – How to edit a text file, to put there the text that you want. – How to store a text file in the folder that you want. – How to figure out the path of the folder where a file is stored. If you do not know how to do these things, talk to the instructor or the TA. 3

Making Files Visible to Netbeans From Netbeans: – Click on File->Project Properties. – At the top, see where the project folder is. – In order for your Java code to see a text file, you should put the text file on the project folder. IF YOU DO NOT FOLLOW THESE STEPS, YOUR FILE-RELATED PROGRAMS WILL NOT WORK. 4

5 Folder where your project is saved:

Motivation for Files: Phonebook Consider a phonebook application, that allows: – Making a new entry (new name and phone number). – Modifying an existing entry. – Deleting an entry. – Looking up the phone given the name. – Looking up the name given the phone. What can we do and what can we not do, using what we have learned so far? 6

Consider a phonebook application, that allows: – Making a new entry (new name and phone number). – Modifying an existing entry. – Deleting an entry. – Looking up the phone given the name. – Looking up the name given the phone. We can do all five things listed above. However, at the end of the program, all information vanishes. Files provides a solution: – data can be saved into files before the program exits. – data can be read again from those files when needed. 7 Motivation for Files: Phonebook

Motivation for Files: Spreadsheets Spreadsheets are one of the most important computer applications. Spreadsheets organize data in tables, consisting of rows and columns. 8

Spreadsheet Example: Class Enrollments 9 Date /14/ /17/ /24/ /29/ /2/ /5/ /7/ An enrollment spreadsheet shows how many students are enrolled each day in each course section. Such a spreadsheet typically includes tens of rows (dates) and hundreds of columns (courses).

Spreadsheet Example: Class Enrollments 10 Here is the table from the previous slide, saved as a text file. This is a CSV file. CSV stands for "comma-separated values". Such files are really easy to read by computer programs. – You will soon write such programs. Date, , , /14/2015,33,41,21 7/17/2015,41,41,20 7/24/2015,50,42,18 7/29/2015,57,41,19 8/2/2015,58,41,18 8/5/2015,67,41,19 8/7/2015,72,41,17

Motivation for Files: Large Text Processing Suppose that we have to write a program that: – takes a book (or a set of books) as an input. – identifies the most frequent words in that book or set of books. Can you think of example applications for such a program? 11

Motivation for Files: Large Text Processing Suppose that we have to write a program that: – takes a book (or a set of books) as an input. – identifies the most frequent words in that book or set of books. Can you think of example applications for such a program? – identifying the most important words to learn in a foreign language. – identifying the language in which a book was written. – identifying and comparing style of different authors, newspapers, centuries, etc. 12

Motivation for Files: s, Web Pages s and web pages are stored as files. Programs are needed to: – Create such files, and store data appropriately. – Read such files, and present data to the user. 13

Motivation for Files: Images, Video, Songs This is beyond the scope of this course, but: Photographs, movies, songs, are all stored in files. Programs are needed to read such files and interpret the data: – Display photos. – Play movies. – Play songs. Programs are also needed to create such files (store images, video, songs in files). 14

File Format 15 The file format specifies how to interpret the data. In some cases, we need to know the format in advance, in order to to understand how to process the data. In the above example (from the enrollments spreadsheet), the CSV format is followed. Date, , , /14/2015,33,41,21 7/17/2015,41,41,20 7/24/2015,50,42,18 7/29/2015,57,41,19 8/2/2015,58,41,18 8/5/2015,67,41,19 8/7/2015,72,41,17

File Format 16 On the right, we see the same data stored in another format (columns separated by _, i.e., the underscore character). We need to know the format in advance, in order to to understand how to process the data shown above. Date, , , /14/2015,33,41,21 7/17/2015,41,41,20 7/24/2015,50,42,18 7/29/2015,57,41,19 8/2/2015,58,41,18 8/5/2015,67,41,19 8/7/2015,72,41,17 Date_ _ _ /14/2015_33_41_21 7/17/2015_41_41_20 7/24/2015_50_42_18 7/29/2015_57_41_19 8/2/2015_58_41_18 8/5/2015_67_41_19 8/7/2015_72_41_17

First Example 17 import java.util.Scanner; import java.io.File; public class enrollments { public static void print_file(String filename) { File temp = new File(filename); Scanner input_file; try { input_file = new Scanner(temp); } catch (Exception e) { System.out.printf("Failed to open file %s\n", filename); return; } while(input_file.hasNextLine()) { String line = input_file.nextLine(); System.out.printf("%s\n", line); } input_file.close(); } public static void main(String[] args) { print_file("enrollments.csv"); } This is our first example of a program reading a file. Not much processing of data happens here. The code simply reads lines of text and prints them. Note that you need to import java.io.File.

Steps in Reading a File 18 import java.util.Scanner; import java.io.File; public class enrollments { public static void print_file(String filename) { File temp = new File(filename); Scanner input_file; try { input_file = new Scanner(temp); } catch (Exception e) { System.out.printf("Failed to open file %s\n", filename); return; } while(input_file.hasNextLine()) { String line = input_file.nextLine(); System.out.printf("%s\n", line); } input_file.close(); } public static void main(String[] args) { print_file("enrollments.csv"); } First step: opening a file. Two substeps: – Create a File object. – Create a Scanner object. We have been using scanner objects for user input. Scanner objects created as shown can be used to read data from files.

Steps in Reading a File 19 import java.util.Scanner; import java.io.File; public class enrollments { public static void print_file(String filename) { File temp = new File(filename); Scanner input_file; try { input_file = new Scanner(temp); } catch (Exception e) { System.out.printf("Failed to open file %s\n", filename); return; } while(input_file.hasNextLine()) { String line = input_file.nextLine(); System.out.printf("%s\n", line); } input_file.close(); } public static void main(String[] args) { print_file("enrollments.csv"); } Creating the scanner object for a file has to be fine using try... catch. Why?

Steps in Reading a File 20 import java.util.Scanner; import java.io.File; public class enrollments { public static void print_file(String filename) { File temp = new File(filename); Scanner input_file; try { input_file = new Scanner(temp); } catch (Exception e) { System.out.printf("Failed to open file %s\n", filename); return; } while(input_file.hasNextLine()) { String line = input_file.nextLine(); System.out.printf("%s\n", line); } input_file.close(); } public static void main(String[] args) { print_file("enrollments.csv"); } Creating the scanner object for a file has to be fine using try... catch. Why? Because something could go wrong, and the program would crash. For example, the filename could be wrong.

Steps in Reading a File 21 import java.util.Scanner; import java.io.File; public class enrollments { public static void print_file(String filename) { File temp = new File(filename); Scanner input_file; try { input_file = new Scanner(temp); } catch (Exception e) { System.out.printf("Failed to open file %s\n", filename); return; } while(input_file.hasNextLine()) { String line = input_file.nextLine(); System.out.printf("%s\n", line); } input_file.close(); } public static void main(String[] args) { print_file("enrollments.csv"); } Second step: reading the data. There are a lot of ways to do this. However, to simplify, we will only use two methods: – hasNextLine(): check if there is more data to read. – nextLine(): read the next line of data.

Steps in Reading a File 22 import java.util.Scanner; import java.io.File; public class enrollments { public static void print_file(String filename) { File temp = new File(filename); Scanner input_file; try { input_file = new Scanner(temp); } catch (Exception e) { System.out.printf("Failed to open file %s\n", filename); return; } while(input_file.hasNextLine()) { String line = input_file.nextLine(); System.out.printf("%s\n", line); } input_file.close(); } public static void main(String[] args) { print_file("enrollments.csv"); } Second step: reading the data. Notice that we read the data using a while loop. – hasNextLine() will return false when there are no more lines to read from the file.

Steps in Reading a File 23 import java.util.Scanner; import java.io.File; public class enrollments { public static void print_file(String filename) { File temp = new File(filename); Scanner input_file; try { input_file = new Scanner(temp); } catch (Exception e) { System.out.printf("Failed to open file %s\n", filename); return; } while(input_file.hasNextLine()) { String line = input_file.nextLine(); System.out.printf("%s\n", line); } input_file.close(); } public static void main(String[] args) { print_file("enrollments.csv"); } Third step: process the data. This task depends on what you want to do. In this simple example, we just print the data.

Steps in Reading a File 24 import java.util.Scanner; import java.io.File; public class enrollments { public static void print_file(String filename) { File temp = new File(filename); Scanner input_file; try { input_file = new Scanner(temp); } catch (Exception e) { System.out.printf("Failed to open file %s\n", filename); return; } while(input_file.hasNextLine()) { String line = input_file.nextLine(); System.out.printf("%s\n", line); } input_file.close(); } public static void main(String[] args) { print_file("enrollments.csv"); } Fourth step: close the file. Use the close() method.

Reading a File: Summary 25 First step: open the file. – Make sure you know how to create a File object. – Make sure you know how to create a Scanner object from a File object. Second step: read the data. – Make sure you know how to use hasNextLine() and nextLine(). Third step: process the data. – This is task-dependent, you do whatever is needed. Fourth step: close the file. – Make sure you know how to use the close() method.

Storing File Contents in a Variable 26 Many times we need to store the data of the entire file in a variable, to do more processing. – For example, to edit a spreadsheet. We can store the data from the entire file in an array list of strings. – A string for each line of text in the file. Why an array list and not an array?

Storing File Contents in a Variable 27 Many times we need to store the data of the entire file in a variable, to do more processing. – For example, to edit a spreadsheet. We can store the data from the entire file in an array list of strings. – A string for each line of text in the file. Why an array list and not an array? – First, because we don't know from the beginning how many lines the file has. – Second, because this way we can insert more lines if we want (for example, to add data to a spreadsheet).

Storing File Contents in a Variable 28 Many times we need to store the data of the entire file in a variable, to do more processing. – For example, to edit a spreadsheet. We can store the data from the entire file in an array list of strings. – A string for each line of text in the file. Let's write a function read_file that: – Takes as input a filename. – Returns an array list of all the lines of text on the file.

29 public static ArrayList read_file(String filename) { File temp = new File(filename); Scanner input_file; try { input_file = new Scanner(temp); } catch (Exception e) { System.out.printf("Failed to open file %s\n", filename); return null; } ArrayList result = new ArrayList (); while(input_file.hasNextLine()) { String line = input_file.nextLine(); result.add(line); } input_file.close(); return result; } The solution is almost identical to our print_file function. The new lines are shown in red.

Using the read_file Function 30 Let's rewrite function file_print, so that it uses the read_file function that we just wrote. public static void print_file(String filename) { ArrayList lines = read_file(filename); if (lines == null) { return; } for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); System.out.printf("%s\n", line); }

31 Let's write a function file_length that: – Takes as input a filename. – Returns the number of characters in that file. Hint: use the read_file function that we already have. A Second Example: Length of a File

32 public static int file_length(String filename) { ArrayList lines = read_file(filename); if (lines == null) { return 0; } int length = 0; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); length += line.length(); } return length; } A Second Example: Length of a File

33 Let's write a function count_words that: – Takes as input a filename. – Returns the number of words in that file. Question: how can we count the number of words in a string of text? A Third Example: Counting Words

34 Let's write a function count_words that: – Takes as input a filename. – Returns the number of words in that file. Question: how can we count the number of words in a string of text? Words are typically separated by space. However, they could also be separated by commas, periods, and other punctuation. The String.split method can be used to split text into words. A Third Example: Counting Words

The split Method The split method separates a string into an array of "words" (smaller strings). Argument: a string specifying the characters that separate the words. 35 public class example { public static void main(String[] args) { String text = "Today is a hot summer day."; String[] words = text.split(" "); for (int i = 0; i < words.length; i++) { System.out.printf("word[%d] = %s\n", i, words[i]); } Output: words[0] = Today words[1] = is words[2] = a words[3] = hot words[4] = summer words[5] = day.

The split Method You can specify multiple characters that separate words, as seen in this example. In the example below, we say that words are separated by comma, space, and dash. Important: to specify multiple characters, you must enclose them in square brackets: []. 36 public class example { public static void main(String[] args) { String text = "Let's count: One,two,three."; String[] words = text.split("[, -]"); for (int i = 0; i < words.length; i++) { System.out.printf("word[%d] = %s\n", i, words[i]); } Output: word[0] = Let's word[1] = count: word[2] = One word[3] = two word[4] = three.

The split Method You can specify multiple characters that separate words, as seen in this example. In the example below, we say that words are separated by comma, space, and dash. Important: to specify multiple characters, you must enclose them in square brackets: [] (see the example argument: "[, -]"). 37 public class example { public static void main(String[] args) { String text = "Let's count: One,two,three."; String[] words = text.split("[, -]"); for (int i = 0; i < words.length; i++) { System.out.printf("word[%d] = %s\n", i, words[i]); } Output: word[0] = Let's word[1] = count: word[2] = One word[3] = two word[4] = three.

38 public static int count_words(String filename) { ArrayList lines = read_file(filename); if (lines == null) { return 0; } int counter = 0; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); String[] words = line.split("[,.- ]"); counter += words.length; } return counter; } A Third Example: Counting Words Here we specify that words are separated by commas, periods, dashes, spaces.

Writing Data to a File We saw that, to read from a file, we associate a file with a Scanner object. – Then, we read from a file exactly as we read user input. To write to a file, we associate a file with a PrintWriter object. – Then, we print to the file exactly as we print user input. – We can use printf, println, and so on. 39

40 import java.io.PrintWriter; public class file_writing_example { public static void main(String[] args) { String filename = "out1.txt"; PrintWriter out = null; try { out = new PrintWriter(filename); } catch (Exception e) { System.out.printf("Error: failed to open file %s.\n", filename); System.exit(0); } out.printf("writing a line to a file.\n"); out.printf("writing a second line.\n"); out.close(); System.out.printf("Done writing to file %s.\n", filename); } Writing Data to a File Step 1: Make sure you import java.io.PrintWriter.

41 import java.io.PrintWriter; public class file_writing_example { public static void main(String[] args) { String filename = "out1.txt"; PrintWriter out = null; try { out = new PrintWriter(filename); } catch (Exception e) { System.out.printf("Error: failed to open file %s.\n", filename); System.exit(0); } out.printf("writing a line to a file.\n"); out.printf("writing a second line.\n"); out.close(); System.out.printf("Done writing to file %s.\n", filename); } Writing Data to a File Step 2: Initialize PrintWriter object to null (so that we can associate it with a file using try … catch).

42 import java.io.PrintWriter; public class file_writing_example { public static void main(String[] args) { String filename = "out1.txt"; PrintWriter out = null; try { out = new PrintWriter(filename); } catch (Exception e) { System.out.printf("Error: failed to open file %s.\n", filename); System.exit(0); } out.printf("writing a line to a file.\n"); out.printf("writing a second line.\n"); out.close(); System.out.printf("Done writing to file %s.\n", filename); } Writing Data to a File Step 3: Associate the PrintWriter object with a file. We need to use try … catch, to catch the case where for some reason the file could not be opened (invalid filename, read-only file, etc).

43 import java.io.PrintWriter; public class file_writing_example { public static void main(String[] args) { String filename = "out1.txt"; PrintWriter out = null; try { out = new PrintWriter(filename); } catch (Exception e) { System.out.printf("Error: failed to open file %s.\n", filename); System.exit(0); } out.printf("writing a line to a file.\n"); out.printf("writing a second line.\n"); out.close(); System.out.printf("Done writing to file %s.\n", filename); } Writing Data to a File Step 4: Write whatever data you want. Note that we use out.printf, and NOT System.out.printf (which would just print to the screen).

44 import java.io.PrintWriter; public class file_writing_example { public static void main(String[] args) { String filename = "out1.txt"; PrintWriter out = null; try { out = new PrintWriter(filename); } catch (Exception e) { System.out.printf("Error: failed to open file %s.\n", filename); System.exit(0); } out.printf("writing a line to a file.\n"); out.printf("writing a second line.\n"); out.close(); System.out.printf("Done writing to file %s.\n", filename); } Writing Data to a File Step 5: close the file. If you forget this step, some or all your data may not be saved in the file.

45 import java.io.PrintWriter; public class file_writing_example { public static void main(String[] args) { String filename = "out1.txt"; PrintWriter out = null; try { out = new PrintWriter(filename); } catch (Exception e) { System.out.printf("Error: failed to open file %s.\n", filename); System.exit(0); } out.printf("writing a line to a file.\n"); out.printf("writing a second line.\n"); out.close(); System.out.printf("Done writing to file %s.\n", filename); } Writing Data to a File Question: what happens if your file already contained some data, before you ran this program?

46 import java.io.PrintWriter; public class file_writing_example { public static void main(String[] args) { String filename = "out1.txt"; PrintWriter out = null; try { out = new PrintWriter(filename); } catch (Exception e) { System.out.printf("Error: failed to open file %s.\n", filename); System.exit(0); } out.printf("writing a line to a file.\n"); out.printf("writing a second line.\n"); out.close(); System.out.printf("Done writing to file %s.\n", filename); } Writing Data to a File Question: what happens if your file already contained some data, before you ran this program? Answer: the previous data is lost.

47 import java.io.PrintWriter; public class file_writing_example { public static void main(String[] args) { String filename = "out1.txt"; PrintWriter out = null; try { out = new PrintWriter(filename); } catch (Exception e) { System.out.printf("Error: failed to open file %s.\n", filename); System.exit(0); } out.printf("writing a line to a file.\n"); out.printf("writing a second line.\n"); out.close(); System.out.printf("Done writing to file %s.\n", filename); } Problem Viewing the Output File On Windows, if you open the text file using Notepad, it shows as one long line.

48 import java.io.PrintWriter; public class file_writing_example { public static void main(String[] args) { String filename = "out1.txt"; PrintWriter out = null; try { out = new PrintWriter(filename); } catch (Exception e) { System.out.printf("Error: failed to open file %s.\n", filename); System.exit(0); } out.printf("writing a line to a file.\r\n"); out.printf("writing a second line.\r\n"); out.close(); System.out.printf("Done writing to file %s.\n", filename); } Problem Viewing the Output File On Windows, if you open the text file using Notepad, it shows as one long line. To fix this problem, use \r\n instead of \n for a newline.

Writing Data to a File: Recap Step 1: Make sure you import java.io.PrintWriter. Step 2: Initialize a PrintWriter variable called out to null (so that we can associate it with a file using try … catch). Step 3: Associate the PrintWriter variable with a file. – We need to use try … catch, to catch the case where for some reason the file could not be opened (invalid filename, read-only file, etc). Step 4: Write whatever data you want. – Use out.printf, and NOT System.out.printf (which would just print to the screen). Step 5: close the file. – If you forget this step, some or all your data may not be saved in the file. 49

Example: Convert to Squares Write a function save_squares(input_name, output_name) that: – Reads numbers from a file named input_file that just contains numbers, one per line. – Writes to a file named output_file the square of each number that it read from the input file. 50

Example: Convert to Squares Logic: Read the lines from the input file. Open output file. – For each line in the input: – Convert string to double. – Compute the square. – Save the square to the output file. Close the output file. 51

52 public static void save_squares(String input_file, String output_file) { ArrayList lines = read_file(input_file); PrintWriter out = null; try { out = new PrintWriter(output_file); } catch (Exception e) { System.out.printf("Error: failed to open %s.\n", output_file); return; } for (int i = 0; i < lines.size(); i++) { double number; try { number = Double.parseDouble(lines.get(i)); } catch (Exception e) { System.out.printf("Error: %s is not a number.\n", lines.get(i)); return; } out.printf("%.2f\r\n", number * number); } out.close(); }

Spreadsheets 53 Date /14/ /17/ /24/ /29/ /2/ /5/ /7/ A spreadsheet is a table. – You can specify a row and a column, and you get back a value. For example, in the above spreadsheet: – You specify a row (date) and a column (course). – You get back the number of students enrolled in that course on that date.

Spreadsheets in CSV Format 54 Here is the table from the previous slide, saved as a CSV file. – CSV stands for "comma-separated values". CSV is an example of a file format. A file format is a convention on how to store data in a file. To write code that reads data from a file, a programmer must always know the format (convention) that is being used. In this class, we will use the CSV format for saving spreadsheets on files. Date, , , /14/2015,33,41,21 7/17/2015,41,41,20 7/24/2015,50,42,18 7/29/2015,57,41,19 8/2/2015,58,41,18 8/5/2015,67,41,19 8/7/2015,72,41,17

Storing a Spreadsheet in a Variable 55 A spreadsheet is a table. – You can specify a row and a column, and you get back a value. When your code reads a spreadsheet, it is convenient to store the entire spreadsheet into a single variable. The spreadsheet is a sequence of rows, and thus can be stored as an array (or array list) of rows. Each row is a sequence of values, and thus can be stored as an array (or array list) those values. – Each value is a string. Therefore, what type of variable do we need for storing a spreadsheet?

Storing a Spreadsheet in a Variable 56 A spreadsheet is a table. – You can specify a row and a column, and you get back a value. When your code reads a spreadsheet, it is convenient to store the entire spreadsheet into a single variable. The spreadsheet is a sequence of rows, and thus can be stored as an array (or array list) of rows. Each row is a sequence of values, and thus can be stored as an array (or array list) those values. – Each value is a string. Therefore, what type of variable do we need for storing a spreadsheet? – An array (or array list) of arrays (or array lists) of strings.

Storing a Spreadsheet in a Variable 57 Therefore, we have a few choices on how to store spreadsheet data in a variable. Our variable can be: – An array of arrays of strings. – An array of array lists of strings. – An array list of arrays of strings. – An array list of array lists of strings.

Storing a Spreadsheet in a Variable We will implement two of those options. We will write functions that read a spreadsheet and store its data as: – An array of arrays of strings. – An array list of array lists of strings. 58

Storing a Spreadsheet in a 2D Array 59 If we store a spreadsheet as an array of arrays of strings, the variable looks like this: String[][] data. – data[i] corresponds to the i-th line of the spreadsheet. – data[i] contains an array of strings. – data[i][j] holds the spreadsheet value for row i, column j. Let's write a function read_spreadsheet that: – Takes as input a filename. – Returns an array of arrays of strings storing all the data in the spreadsheet.

Storing a Spreadsheet in a 2D Array 60 public static String[][] read_spreadsheet(String filename) { ArrayList lines = read_file(filename); if (lines == null) { return null; } // The row below creates an array of length "rows", that stores // objects of type String[]. Those objects are initialized to null. String[][] result = new String[lines.size()][]; for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); result[i] = line.split(","); } return result; }

Storing a Spreadsheet in a 2D ArrayList 61 If we store a spreadsheet as an array list of array lists of strings, the variable looks like this: ArrayList > data. – data.get(i) corresponds to the i-th line of the spreadsheet. – data.get(i) is an array list of strings. – data.get(i).get(j) holds the spreadsheet value for row i, column j. Let's write a function read_spreadsheet2 that: – Takes as input a filename. – Returns an array list of array lists of strings storing all the data in the spreadsheet.

62 public static ArrayList > read_spreadsheet2(String filename) { ArrayList lines = read_file(filename); if (lines == null) { return null; } ArrayList > result = new ArrayList(); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); String[] values = line.split(","); ArrayList list = new ArrayList(); for (int j = 0; j < values.length; j++) { list.add(values[j]); } result.add(list); } return result; } Storing a Spreadsheet in a 2D ArrayList

Example Programs On the lectures web page, you can access two programs that use these read_spreadsheet functions. – nba_leaders_2d_array_version.java stores data as a 2D array of strings. – nba_leaders_2d_arraylist_version.java stores data as a 2D array list of strings. Both programs have exactly the same functionality: – They read a spreadsheet of statistics of about 270 basketball players. – Then, the user can specify a column, and the program prints out the player (or multiple players, in case of ties) with the best statistics in that column. 63