Scala File I/O. Types of files There are two kinds of files: Text files, and binary files Of course, it’s not that simple… Text files Text files are “human.

Slides:



Advertisements
Similar presentations
The Windows File System and Windows Explorer To move around the file system and examine your files or get to one you want (say, to modify, delete or copy.
Advertisements

A batch file is a file that contains a number of DOS commands, each of which could be run individually from the command prompt. By putting them into a.
A Guide to Unix Using Linux Fourth Edition
Chapter 4 Marking Up With Html: A Hypertext Markup Language Primer.
CS 898N – Advanced World Wide Web Technologies Lecture 8: PERL Chin-Chih Chang
Review of HTML (adapted from Guthrie, , last updated January, 2007)
Binary Expression Numbers & Text CS 105 Binary Representation At the fundamental hardware level, a modern computer can only distinguish between two values,
Chapter 5 Accessing Files and Directories. How Directories Get Created OS installation: usr, dev, etc, export, kernel and others places to store installation.
Python and Web Programming
28-Jun-15 Number Systems. 2 Bits and bytes A bit is a single two-valued quantity: yes or no, true or false, on or off, high or low, good or bad One bit.
Marking Up With Html: A Hypertext Markup Language Primer
Characters and Strings. Characters In Java, a char is a primitive type that can hold one single character A character can be: –A letter or digit –A punctuation.
Computer Arithmetic: Binary, Octal and Hexadecimal Presented by Frank H. Osborne, Ph. D. © 2005 ID 2950 Technology and the Young Child.
ASCII and Unicode. ASCII Inside a computer, EVERYTHING is a number – that includes music, sound, and text. In the early days of computers, every manufacturer.
CHARACTERS Data Representation. Using binary to represent characters Computers can only process binary numbers (1’s and 0’s) so a system was developed.
Primaries: magenta, yellow, and cyan This color system is called subtractive because: each primary color absorbs (subtracts) a certain part of the color.
Navigating the Information Highway’s Fast Lane A Speed Demon’s Guide to the Internet.
Chapter 4 Fluency with Information Technology L. Snyder Marking Up With HTML: A Hypertext Markup Language Primer.
CS0007: Introduction to Computer Programming File IO and Recursion.
Ch 51 Internal Commands COPY and TYPE. Ch 52 Overview Will review file-naming rules.
Agenda Data Representation – Characters Encoding Schemes ASCII
Text Processing. Outline Announcements: –Homework I: due Today. by 5, by Discuss on Friday. –Homework II: on web HW I: question 7 Finish functions.
Chapter Three The UNIX Editors. 2 Lesson A The vi Editor.
FILES & FOLDERS Organization Computer hard drives hold an enormous amount of data or information. Knowing how a computer's organization system works.
Copyright © 2009 Curt Hill The Picture Object Getting and displaying.
Text and Graphics September 26, Unit 3.
1 The EDIT Program The Edit program is a full screen text editor that allows you to: Create text files Create text files Edit an existing text files Edit.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
Robert Crawford, MBA West Middle School.  Explain how the binary system is used by computers.  Describe how software is written and translated  Summarize.
Computer Programming I Hour 2 - Writing Your First C Program.
4 Chapter Four Introduction to HTML. 4 Chapter Objectives Learn basic HTML commands Discover how to display graphic image objects in Web pages Create.
Data Files on Computers Text Files (ASCII) Files that can be created by typing on the keyboard while using a text editor such as notepad or TextEdit.
File I/O Ruth Anderson UW CSE 160 Spring File Input and Output As a programmer, when would one use a file? As a programmer, what does one do with.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
CS 330 Programming Languages 10 / 02 / 2007 Instructor: Michael Eckmann.
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.
Chapter 9 1 Chapter 9 – Part 2 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Text Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
Introduction to Computer Programming - Project 1 Intro to Digital Technology.
File Paths *referenced from
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
CSC 110 – Intro to Computing - Programming
CS 177 Recitation Week 1 – Intro to Java. Questions?
Copyright © Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.
17-Mar-16 Characters and Strings. 2 Characters In Java, a char is a primitive type that can hold one single character A character can be: A letter or.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Eight: Streams Slides by Evan Gallagher.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
Lecture Coding Schemes. Representing Data English language uses 26 symbols to represent an idea Different sets of bit patterns have been designed to represent.
Unit 2.6 Data Representation Lesson 2 ‒ Characters
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Topic: File Input/Output (I/O)
COMP Streams and File I/O
NUMBER SYSTEMS.
Arrays and files BIS1523 – Lecture 15.
Data Encoding Characters.
TOPICS Information Representation Characters and Images
Representing Characters
File IO and Strings CIS 40 – Introduction to Programming in Python
File I/O ICS 111: Introduction to Computer Science I
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Introduction to Primitive Data types
Fundamentals of Data Representation
Digital Encodings.
Learning Intention I will learn how computers store text.
Lab 5: Complex Inputs.
Lab 3: File Permissions.
Networks & I/O Devices July 10, 2019.
Introduction to Primitive Data types
ASCII and Unicode.
Varying Character Lengths
Presentation transcript:

Scala File I/O

Types of files There are two kinds of files: Text files, and binary files Of course, it’s not that simple… Text files Text files are “human readable,” which really means that a lot of different programs can understand them ASCII (American Standard Code for Information Interchange) has been in widespread use since about 1967 ASCII is a set of 128 character, many of which are “control” characters Unicode was designed to support “all” the world’s languages, and began to be used in about 1987 UTF-8 (UCS Transformation Format—8-bit) combines ASCII and Unicode, and has become the Web standard Binary files Binary files are not designed to be human readable, and are often ad hoc Some familiar binary file types are jpg, gif, png, doc, rtf, class We’ll only be concerned with text files 2

How to refer to a file There are four ways to tell the computer where to find a file (for reading) or put a file (for writing) Provide the absolute path to the file For those of you on Windows, this usually means starting with C: This is almost always a terrible idea! It means that anyone you give your program to (like, the instructor) must have the exact same directory structure as you do Provide a relative path to the file (starting from the location in which the program is running) This is not too bad, if you provide a complete directory containing both the program and its data It does require the instructor to start the program from the same place you do Tell the operating system to keep the file in a special hidden location This is good for default settings, but not for data Ask the user where the file is (often the best approach) 3

How to ask the user Do this: import scala.io.Source, scala.io.Source._, scala.swing.FileChooser If you want to read in a file: val chooser = new FileChooser val response = chooser.showOpenDialog(null) (User selects a file) if (response == FileChooser.Result.Approve) { read in file } If you want to write out a file: val chooser = new FileChooser val response = chooser.showSaveDialog(null) (User selects a file) if (response == FileChooser.Result.Approve) { write out file } 4

Which file was chosen? The result of calling FileChooser is either FileChooser.Result.Approve, or it’s something else If it’s Approve, you can ask it which file was chosen with chooser.selectedFile 5

Accessing files by path import java.io.File val file = new File("C:/Programming/p1.txt" ) This gives you a file object that represents a file There is no guarantee that there is such a file All the world, except Microsoft, uses forward slashes ( / ) as path delimiters Microsoft standard is backslashes, \ In a string, backslashes must be doubled: ("C:\\Programming\\p1.txt") Microsoft has ”seen the light” and now allows forward slashes All the world, except Microsoft, uses case-sensitive file names Your TAs aren’t using Windows 6

Reading from a file Either Source.fromFile( file ) or Source.fromFile( filename ) will give you an iterator for the file The iterator will give you one character at a time Thus, for (ch <- Source.fromFile( file )) { print } will print out the entire file, one character at a time Newlines are included (ch <- Source.fromFile( file ).getLines) { print } will print out the entire file, one line at a time Newlines are discarded 7

Putting it all together scala> import scala.io.Source, scala.io.Source._ import scala.io.Source import scala.io.Source._ scala> import scala.swing.FileChooser, java.io.File import scala.swing.FileChooser import java.io.File scala> val chooser = new FileChooser chooser: scala.swing.FileChooser = scala> val response = chooser.showOpenDialog(null) response: scala.swing.FileChooser.Result.Value = Approve scala> if (response == FileChooser.Result.Approve) { | for (line <- Source.fromFile(chooser.selectedFile).getLines) { | println("Processing line: " + line) | } | } 8

The End 9