Working with Files. Learning Objectives By the end of this lecture, you should be able to: – Examine a file that contains code with unfamiliar material,

Slides:



Advertisements
Similar presentations
Modifying existing content Adding/Removing content on a page using jQuery.
Advertisements

Events Part III The event object. Learning Objectives By the end of this lecture, you should be able to: – Learn to use the hover() function – Work with.
The Web Warrior Guide to Web Design Technologies
Form Validation. Learning Objectives By the end of this lecture, you should be able to: – Describe what is meant by ‘form validation’ – Understand why.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 7 Event-Driven.
Escaping Characters document.write(). Learning Objectives By the end of this lecture, you should be able to: – Recognize some of the characters that can.
Introduction to JavaScript. Aim To enable you to write you first JavaScript.
Introduction to scripting
Internet Skills An Introduction to HTML Alan Noble Room 504 Tel: (44562 internal)
1 Chapter 6 – Creating Web Forms and Validating User Input spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Adding JavaScript (<script tag>)
 2003 Prentice Hall, Inc. All rights reserved. CHAPTER 3 JavaScript 1.
Using the API. Learning Objectives By the end of this lecture, you should be able to: – Identify what is meant by an ‘API’ – Know how to look up functions.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
SERVER web page repository WEB PAGE instructions stores information and instructions BROWSER retrieves web page and follows instructions Server Web Server.
CSC 330 E-Commerce Teacher Ahmed Mumtaz Mustehsan Ahmed Mumtaz Mustehsan GM-IT CIIT Islamabad GM-IT CIIT Islamabad CIIT Virtual Campus, CIIT COMSATS Institute.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
PHP meets MySQL.
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Extending HTML CPSC 120 Principles of Computer Science April 9, 2012.
Dynamic Web Pages & JavaScript. Dynamic Web Pages Dynamic = Change Dynamic Web Pages are web pages that change. More than just moving graphics around.
Forms and Server Side Includes. What are Forms? Forms are used to get user input We’ve all used them before. For example, ever had to sign up for courses.
Diagnostic Pathfinder for Instructors. Diagnostic Pathfinder Local File vs. Database Normal operations Expert operations Admin operations.
Java server pages. A JSP file basically contains HTML, but with embedded JSP tags with snippets of Java code inside them. A JSP file basically contains.
Introduction to JavaScript CS101 Introduction to Computing.
1 A Balanced Introduction to Computer Science David Reed, Creighton University ©2005 Pearson Prentice Hall ISBN X Chapter 4 JavaScript and.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 17 JavaScript.
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.
Modifying HTML attributes and CSS values. Learning Objectives By the end of this lecture, you should be able to: – Select based on a class (as opposed.
Working with Strings. Learning Objectives By the end of this lecture, you should be able to: – Appreciate the need to search for and extract information.
CSD 340 (Blum)1 Starting JavaScript Homage to the Homage to the Square.
Files Tutor: You will need ….
1 CSC160 Chapter 7: Events and Event Handlers. 2 Outline Event and event handlers onClick event handler onMouseOver event handler onMouseOut event handler.
Making dynamic pages with javascript Lecture 1. Java script java versus javascript Javascript is a scripting language that will allow you to add real.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
IS2802 Introduction to Multimedia Applications for Business Lecture 8: JavaScript and Cookies Rob Gleasure
Changing HTML Attributes each() function Anonymous Functions $(this) keyword.
Review of HTML and CSS A (very) brief review of some key fundamentals to be aware of in IT-238.
JavaScript. JavaScript Introduction JavaScript is the world's most popular programming language. It is the language for HTML and the web, for servers,
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 2: Introduction to IS2803 Rob Gleasure
1 HTML Forms
JavaScript Introduction and Background. 2 Web languages Three formal languages HTML JavaScript CSS Three different tasks Document description Client-side.
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.
1 CSC160 Chapter 1: Introduction to JavaScript Chapter 2: Placing JavaScript in an HTML File.
CSC 121 Computers and Scientific Thinking Fall Event-Driven Programming.
JavaScript and Ajax (JavaScript Environment) Week 6 Web site:
1 JavaScript and Dynamic Web Pages Lecture 7. 2 Static vs. Dynamic Pages  A Web page uses HTML tags to identify page content and formatting information.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
 2001 Prentice Hall, Inc. All rights reserved. Outline 1 JavaScript.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Moving away from alert() Using innerHTML Using an empty div section
Arrays: Checkboxes and Textareas
* Lecture # 7 Instructor: Rida Noor Department of Computer Science
Retrieving information from forms
Intro to PHP & Variables
File Handling Programming Guides.
In Class Program: Today in History
T. Jumana Abu Shmais – AOU - Riyadh
Fundamentals of Data Structures
Functions, Regular expressions and Events
Chapter 17 JavaScript Arrays
Working with Strings.
Events Part III The event object.
Reading information from files
Retrieving information from forms
Presentation transcript:

Working with Files

Learning Objectives By the end of this lecture, you should be able to: – Examine a file that contains code with unfamiliar material, yet still be able to study and modify it as needed to suit your purposes. This includes: How to open a file to read information How to use a loop How to use array (i.e. square bracket) notation – Read in text from a file and use various String functions to work with the information read in from a file

Modifying Existing Code Early in your coding "careers", as you progress, you will frequently find yourself working with code that is unfamiliar to you. This is entirely normal and is, in some ways, a "next level" of developing solid coding skills. In this lecture, I will be showing you some examples that contain new and unfamiliar code. You will not be asked to work in great deal with the code, but you will be expected to study it and learn enough about it to modify the code to suit your needs. You are, of course, welcome and encouraged to try and read up on some of these concepts on your own. Again, this is how programmers have traditionally gained new knowledge, skills, and picked up new clever ways of accomplishing coding tasks.

Working with Files Most of the world’s digital information is stored inside files. Data files can include anything from a spreadsheet, database file, to a plain text file. Even digital images constitute data files that can be opened using programming code and examined or modified. We can use JavaScript to retrieve information from a digital file and use the information in that file to interact with our user. In this lecture, we will keep it fairly simple by opening a text file and outputting its contents to a web page. In the process, we will also review some issues pertaining to JavaScript security.

Scenario Imagine that you would like to take a detailed spreadsheet and output the results to your web page. Example: For example, suppose you wanted to display the results of the Kentucky Derby in an HTML table. You could manually type out all the results, but this would be painstaking, error-prone, and very inefficient! Besides, imagine if instead of a horse-race with horses, you instead were trying to print out the first 500 finishers of the Chicago Marathon! Now consider an alternative: You find a spreadsheet of the results (or just copy-paste), and save that file to your server. You can then use JavaScript to retrieve the contents of that file and display the results on your web page.

Example: Student Enrollment Let’s start with a simple example involving student enrollment. You have been given an Excel spreadsheet with a list of students names, year of birth, year of enrollment, and current registration status (i.e. full time v.s. part-time). You decide that you need a page that will display the first name, last name, and enrollment status of all the students. Here are the steps we will follow for this particular scenario: 1.Take the Excel spreadsheet and convert it to a comma-separated-value file. This will allow us to use the String ‘ split() ’ function to break up each record into its individual fields. 2.We will read in this file using JavaScript. As mentioned, we will use various string functions to work with the text in the file. 3.We will output the results as simple HTML content into our document. – In a subsequent example, we will output our results to an HTML table. (For those of you who have not seen HTML tables, don’t panic. They are not very dificult, and a 15 minute google search and review should get you up to speed).Remember that one of the objectives is to get to a point where, with a little research and practice, you can pick up new techniques on your own.

Security Issue: File Access As we will discuss in a subsequent lecture, one of JavaScript’s biggest security limitations is that scripts are not allowed to access files on the user’s computer. However, if the user chooses a file, say, by using the HTML tag, then this is considered as having given the script permission to access that file. So we will start by having the user select a file: Click the button and choose the file you want to work with (e.g. enrollment.csv): <input type='file' accept='text/plain' onchange='openFile(event)'> The ‘ onchange ’ event means that when the user selects a file and clicks ‘ Open ’, the function ‘ openFile ’ will be invoked.

File Access: Connecting to and opening the file Remember that the objective here is not to try and understand all of the code. At the end of the day, what I want you to be able to do is to understand the ‘gist’ of what is happening, but then to be able to modify certain key components of the code as necessary to suit your needs for other files and output. var openFile = function(event) { //openFile function; uses an anonymous function with an event object var input = event.target; //The 'target' attribute for a File box refers to the selected file(s) //'input' is a handle to a list of any files the user has selected //Most of the time, of course, the user will only select one file var reader = new FileReader(); //Don't worry about this line for now reader.readAsText(input.files[0]); ///'reader' is a handle to the first file the user selected //For our purposes, we will assume that the user will select only one file Don't try to understand this code just yet. It will make (a little) more sense when we look at it in context. Afterwards you can come back to it for further review.

File Access: Retrieving the file’s contents We will now retrieve the file’s contents:. We are keeping things simple for this example and assuming two things: 1.That we are dealing with a plain-text file 2.The values in the plain-text file are separated by commas var fileContents = reader.result; //'result' is a property that retrieves ALL of the file's contents as a String //We will store this text inside the variable 'fileContents‘ Try appending ‘fileContents’ into your HTML document (e.g. using document.write or via jQuery’s append function) – you will see the entire contents of the file as a single line. Important: In fact, each line is separated from the next by a special text character: \n Text editors use the \n as a way of referring to a new line. However, a web browser does not recognize this character. In HTML as we know, new lines are indicated by. Therefore, you will see that the browser simply display a single space between one line and the next.

Working with the text: Break up each line in the file into records Now that we have all of the file's contents stored as a string inside 'fileContents', var fileContents = reader.result; we will break up that long string of the entire file into an array of individual lines using split(). To do this, we must know that in text files, the end of a line (such as a carriage return or 'enter') is encoded with the string character \n Therefore, by breaking up the string on the newline character \n, we will have an array of smaller strings, each of which represents one line of the file: var lines = fileContents.split('\n');

Working with the text: Break up each line in the file into records contd. Let’s take a closer look at what happens when we invoked ‘split()’ on the newline character: var lines = fileContents.split('\n'); Recall that the split() function breaks up the String ‘ fileContents ’ into numerous pieces. Because we split on the newline character, \n, each piece represents a single line. Therefore, the variable ‘ lines ’ holds an “array”. What this means is that the first item in the variable line, lines[0] holds the first line of the file as a string, the second item, lines[1] holds the second line of the file as a string, and so on. For example, alert( lines[3] );  would output the 4 th line of the file.

Working with the text: Looping through the lines Our variable ‘ lines ’ now contains an array in which each item in lines: lines[0], lines[1], lines[2], etc represents a line from our text file. We will now use a loop to go through each item in our array and output it on a line in our HTML document. How many times do we need to loop? The answer is one time for each line in the file. Remember our ‘lines’ array? Well since that array holds one item for each line, the length of that array tells us the number of lines. So we will use lines.length to determine the number of times to loop. for (var i=0; i<lines.length; i++) { //everything inside here will occur ‘numberOfLines’ times //so if our file had 10 lines, this loop will execute 10 times }

Working with each individual line of text lines[i], represents a single line from our text file. The first time through the loop, I has a value of 0, so lines[i] is the same thing as saying lines[0]. The second time through the loop, i has a value of 1, so lines[i] now is the same thing as saying lines[1], and so on. for (var i=0; i<lines.length; i++) { var record = lines[i]; //record is holding ONE single line from our file recordValues = record.split(","); //Discussed next var firstName = recordValues [0]; var lastName = recordValues [1]; var enrollmentYear = recordValues [3]; $('#results').append ("Student #" + (i+1) + ": "); $('#results').append (firstName + " " + lastName + ": Enrolled in " + enrollmentYear + " "); } //end of for loop

Working with each line of text Recall the first lines of our loop: for (var i=0; i<lines.length; i++) { var record = lines[i]; //record is holding ONE single line from our file recordValues = record.split(","); //Discussed next... Each time we iterate through the loop, ‘record’ holds a single line of the file. Recall that in our file, each line has the record separated by commas as follows: First Name, Last Name, Birth Year, Enrollment Year, Enrollment Status For example: Roberta,Redgrave,1988,2013,FULL We will therefore need use split() again, but this time we want to break the string up based on commas: recordValues = record.split(","); ‘ recordValues ’ now holds an array such that the first name is in recordValues[0], the last name is in recordValues[1], the birth year is in recordValues[2], the enrollment year is in recordValues[3], and the enrollment status is in recordValues[4].

Working with each line of text We will now assign the pieces of information in which we are interested (i.e. the first name, last name and enrollment year) and retrieve them from our line. Also - don’t forget that we are still working with one individual line only. The next time through the loop, we will be working with the subsequent line from the file, and so on. for (var i=0; i<lines.length; i++) { var record = lines[i]; //record is holding a SINGLE line from our file recordValues = record.split(","); var firstName = recordValues [0]; var lastName = recordValues [1]; var enrollmentYear = recordValues [3]; $('#results').append ("Student #" + (i+1) + ": "); $('#results').append (firstName + " " + lastName + ": Enrolled in " + enrollmentYear + " "); } //end of for loop

Outputting the results to our HTML page Finally, we will output the results to our page: for (var i=0; i<lines.length; i++) { var record = lines[i]; //record is holding a SINGLE line from our file recordValues = record.split(","); var firstName = recordValues [0]; var lastName = recordValues [1]; var enrollmentYear = recordValues [3]; $('#results').append ("Student #" + (i+1) + ": "); $('#results').append (firstName + " " + lastName + ": Enrolled in " + enrollmentYear + " "); } //end of for loop

HTML Files See: parsing_input_file.htm Then look at a slightly more elaborate example in which we output results into an HTML table: display_file_as_table.htm