To-Do List Read the Overview for this week to learn what will be covered. Read the Learner Outcomes for this week to learn what you should be able to do when you have completed this week’s work. Complete the Readings for this week, as listed in the Course Assignments and Activities Table in the course syllabus. Complete the assigned reading/s before proceeding to the From the Expert web pages. Read and explore the From the Expert pages for this week. These pages contain critical information and authentic perspectives from experts in this field. Complete this week’s Assignments listed below. See your syllabus for the specific points or percentages for each assignment. Assignments Activity 1: Discussions 1. How would you explain the difference between a file and a folder to someone with little computer experience? 2. When would you have your application use text files as opposed to binary files? 3. What are the differences between a picture, an image, and graphics? Why is there a difference at all? 4. Why is it unwise to assume that you’ll know the dimensions of the Graphics object being sent to a printer? For information on how you will be evaluated, refer to the Discussion Rubric located in the Course Resources folder for easy access throughout this course.
Activity 2: Read Chapter 9 of Intro to Programming with Xojo, completing all the mini exercises and then completing the “Hands on with Files” exercise which starts on page 155. When you have finished, populate your screen with some valid data and make a screen shot of this and put it in a new Word document.
Read week 5 Java Assignment and complete the exercise at the end.
Extra Credit: For extra credit this week, read Chapter 10 of Intro to Programming with Xojo, completing all the mini exercises and then completing the “Hands on with Printing” exercise which starts on page 176. In step 5 of the exercise, make a screen shot of your “choosing the order” and place it in the Word document above.
Submit this completed Word document to the week 5 dropbox.
Activity 3: Complete this CSV Files Assignment to get your prepared for you final exam in week 8.
You will need the Mini-Stores.csv file to complete the assignment.
Week 5 Working with Files with Java This section is to provide an introductory class to create, read, and write to files. The Java language has some classes that are helpful when reading, writing, and creating files. FileReader - is meant for reading streams of characters. For reading streams of raw bytes, consider using. It is for text files in your system’s default encoding. FileInputStream – is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.is used for binary files and files that contain special characters. FileReader (for text files) should usually be wrapped in a BufferedFileReader. This saves up data so you can deal with it a line at a time or whatever instead of character by character.
For writing to a file we will use FileWriter wrapped by the BufferedFileWriter or the FileOutPutStream for binary files. https://docs.oracle.com/javase/tutorial/essential/io/index.html The first thing that we will do is create a file.
In this example we create a new File object. This File object is an abstract representation of file and directory pathnames. The createNewFile() is a boolean method call that will create a file. It will only create it if it does not exist already.
Now that we have a file we will write to it. First, we create a new FileWriter object that will help us with the path of the file. We then pass the file we previously created as a parameter. Notice that after file we used the toString() method. This is because FileWriter takes a string as a parameter not a file type object. Then we created a new BufferedWriter object to pass the fileWriter object which has the file name in it. The new created BufferedWriter object calls the write method that takes a string as a parameter. After we are done we close the BufferedWriter. Now that we have content in the file lets read the content of the file. First, we create a new FileReader object that will help us with the path of the file. We then pass the file we previously created as a parameter. Notice that after file we used the toString() method. This is because FileReader takes a string as a parameter not a file type object. Then we created a new BufferedReader object to pass the fileReader object which has the file name in it. We create a placeholder string called line. This represents the line of text that is reading. Using the While-Loop it will iterate over each line to retrieve the content. In this example we are just printing. Java exercise - create a file and write to it by following the example, by passing input from the user, and by an instantiated
More on Files In the business world, often as an analyst you may be asked to read in a data file and do something with the data. It might be that you’ll simply display the data, perhaps do some calculations or maybe even store the data in a database. A file format which you’ve probably worked with in the past called .CSV, is often used to read the data from. A .CSV file is simply a comma delimited file, that is the pieces of data are separated by a comma. For example, this data came from a CSV file. Store,Sales Turnover,Store Size,Staff Size,Profit Margin New York,5.1,3.5,1.4,0.2 Los Angeles,4.9,3,1.4,0.2 Chicago,4.7,3.2,1.3,0.2
Granted this is a bit hard to read with the human eye, but we can load this into an Excel spreadsheet which will recognize the commas and column separators and display it nicely like this:
Store Sales Turnover Store Size Staff Size Profit Margin New York 5.1 3.5 1.4 0.2 Los Angeles 4.9 3 1.4 0.2 Chicago 4.7 3.2 1.3 0.2
Excel spreadsheets are often used to create .CSV files and likewise read them. But when you’re asked to do more with the data than an Excel spreadsheet can do, you’ll need a program that first can read in the data from a .CSV file. Review the following code:
Dim f As FolderItem Dim textInput As TextInputStream Dim rowFromFile As String Dim Store, SalesTurn, StoreSize, StaffSize, ProfitMargin as String Dim comma As String = ChrB(44) // 44 is the comma ASCII Character f = GetOpenFolderItem("text/plain") // defined as a FileType If f <> Nil And f.Exists Then // if the file exists do the following textInput = TextInputStream.Open(f) // open file textInput.Encoding = Encodings.MacRoman // set to read string; strings are MacRoman End if
While Not textInput.EOF rowFromFile = textInput.ReadLine //read line from the file Store = NthField(rowFromFile, comma, 1) //given that we know there are 5 colums SalesTurn = NthField(rowFromFile, comma, 2) //we can simply hard code this. StoreSize = NthField(rowFromFile, comma, 3) //if column sizes were to change on subsequent StaffSize = NthField(rowFromFile, comma, 4) //files, we would want to get fancy here and ProfitMargin = NthField(rowFromFile, comma, 5) //determine how many columns to read.
// the messages boxes below will display the data that’s been read in, painfully one column of data at a time. msgbox(Store) Msgbox(SalesTurn) MsgBox(StoreSize) Msgbox(StaffSize) MsgBox(ProfitMargin) Wend //loop back and read until end of file (EOF) textInput.Close //close the file
On the first loop of the While statement the first line in the file is read. That line contains the column header names such as Store, Sales Turnover, Stores Size, etc. So, on the first loop the msgbox’s above will display the header names. On the second loop, you’ll be reading the second line of the file, so Store will display New York, Sales Turnover will be 5.1. etc.
Assignment: Download the Mini-Stores.csv that can be found on our class website in week 5. Start a new desktop Xojo project called READCSV. Drag the OK button to your project window and rename it RUN, it should like this:
Double click the run button, go to the action property and paste in the code from above.
Run the program, navigate to the Mini-Stores.CSV that you downloaded above and step through the messages boxes that are displayed.
You now have a wonderful way to read .CSV files.
Note: This does not need to be turned in, but you will need this for your final project so it’s strongly recommended to learn this NOW!