CS1020 Data Structures and Algorithms I Lecture Note #16. File Processing

Similar documents
File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

AP Computer Science File Input with Scanner

Using Files as Input/Output in Java 5.0 Applications

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Files and input/output streams

Line-based file processing

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Object-Oriented Programming in Java

Building Java Programs

Building Java Programs

13 File Output and Input

D06 PROGRAMMING with JAVA

Stream Classes and File I/O

Simple Java I/O. Streams

INPUT AND OUTPUT STREAMS

What is an I/O Stream?

CS 1302 Ch 19, Binary I/O

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

Building Java Programs

Basics of Java Programming Input and the Scanner class

WRITING DATA TO A BINARY FILE

Building a Multi-Threaded Web Server

JAVA - FILES AND I/O

Chapter 2: Elements of Java

Objectives. Streams and File I/O. Objectives, cont. Outline. I/O Overview. Streams

Week 1: Review of Java Programming Basics

Event-Driven Programming

Topic 11 Scanner object, conditional execution

public static void main(string[] args) { System.out.println("hello, world"); } }

Reading Input From A File

Install Java Development Kit (JDK) 1.8

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Introduction to Java

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Chapter 3. Input and output. 3.1 The System class

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Comp 248 Introduction to Programming

B.Sc (Honours) - Software Development

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Chapter 10. A stream is an object that enables the flow of data between a program and some I/O device or file. File I/O

6.1. Example: A Tip Calculator 6-1

The following program is aiming to extract from a simple text file an analysis of the content such as:

Carron Shankland. Content. String manipula3on in Java The use of files in Java The use of the command line arguments References:

Communicating with a Barco projector over network. Technical note

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Software Development in Java

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

CS 106 Introduction to Computer Science I

java Features Version April 19, 2013 by Thorsten Kracht

Creating a Simple, Multithreaded Chat System with Java

Programming in Java

Assignment 4 Solutions

CS 193A. Data files and storage

CS506 Web Design and Development Solved Online Quiz No. 01

Application Development with TCP/IP. Brian S. Mitchell Drexel University

Introduction to Programming

Chapter 2 Introduction to Java programming

Word Count Code using MR2 Classes and API

Programming Languages CIS 443

Course Intro Instructor Intro Java Intro, Continued

Lecture 5: Java Fundamentals III

Input / Output Framework java.io Framework

Division of Informatics, University of Edinburgh

Network/Socket Programming in Java. Rajkumar Buyya

Java from a C perspective. Plan

Mail User Agent Project

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

TP1 : Correction. Rappels : Stream, Thread et Socket TCP

Building Java Programs

CS 111 Classes I 1. Software Organization View to this point:

Zebra and MapReduce. Table of contents. 1 Overview Hadoop MapReduce APIs Zebra MapReduce APIs Zebra MapReduce Examples...

Visit us at

Arrays. Introduction. Chapter 7

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

Quick Introduction to Java

An Overview of Java. overview-1

Introduction to Java. CS 3: Computer Programming in Java

Lecture J - Exceptions

AP Computer Science Static Methods, Strings, User Input

Crash Course in Java

COSC 6397 Big Data Analytics. Distributed File Systems (II) Edgar Gabriel Spring HDFS Basics

Continuous Integration Part 2

File I/O - Chapter 10. Many Stream Classes. Text Files vs Binary Files

IMDB Data Set Topics: Parsing Input using Scanner class. Atul Prakash

This example illustrates how to copy contents from one file to another file. This topic is related to the I/O (input/output) of

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Network Communication

Word count example Abdalrahman Alsaedi

Rationale for Software Architecture

2/1/2010. Background Why Python? Getting Our Feet Wet Comparing Python to Java Resources (Including a free textbook!) Hathaway Brown School

Transcription:

CS1020 Data Structures and Algorithms I Lecture Note #16 File Processing

Objective Input/output on files: reading input from a file and writing data to a file. 2

References Book Chapter 1, Section 1.8, pages 80 to 92 CS1020 website Resources Lectures http://www.comp.nus.edu.sg/ ~cs1020/2_resources/lectures.html 3

Outline 0. Recapitulation 1. File Input 1.1 File Objects 1.2 Reading File 1.3 Input Tokens 1.4 Tokenizing a String 1.5 Exercise: Runners 2. File Output 2.1 PrintStream 2.2 System.out and PrintStream 2.3 Exercise: Runners (revisit) 3. Input and Output Streams 3.1 InputStream and OutputStream 3.2 Examples 4

0. Recapitulation We far we have been using the Scanner class to do interactive input. We have also been using the UNIX input redirection < to redirect data from a file, and output redirection > to redirect data to a file. < and > are UNIX features, not Java s. Now, we will explore how to create File objects in Java. 5

1 File Input

1.1 File Objects (1/2) The API File class represents files In java.io package Creating a File object does not actually create that file on your drive Some methods in File class: Method boolean canread() boolean canwrite() boolean delete() boolean exists() String getname() long length() Description Tests whether the application can read the file Tests whether the application can modify the file Deletes the file or directory Tests whether the file or directory exists Returns the name of the file or directory Returns the length (in bytes) of the file 7

1.1 File Objects (2/2) Example: Path File f = new File("myfile"); if (f.exists() && f.length() > 2048) { f.delete(); Absolute path Specify a drive or start with the root (/) directory Eg: "C:/Documents/CS1020/data" Relative path With respect to where the program resides Eg: "input/eels3.in" 8

1.2 Reading a File (1/3) Pass a File reference when constructing a Scanner object import java.util.*; FileExample1.java import java.io.*; public class FileExample1 { public static void main(string[] args) throws FileNotFoundException { Scanner infile = new Scanner(new File("example")); int sum = 0; while (infile.hasnextint()) { sum += infile.nextint(); System.out.println("Sum = " + sum); File example : 2 7-3 9 1 Output: Sum = 16 9

1.2 Reading a File (2/3) import java.util.*; FileExample2.java import java.io.*; public class FileExample2 { public static void main(string[] args) throws FileNotFoundException { try { Scanner infile = new Scanner(new File("example")); int sum = 0; while (infile.hasnextint()) { sum += infile.nextint(); System.out.println("Sum = " + sum); catch (FileNotFoundException e) { System.out.println("File 'example' not found!"); 10

1.2 Reading a File (3/3) import java.util.*; FileExample3.java import java.io.*; public class FileExample2 { public static void main(string[] args) throws FileNotFoundException { File f = new File("example"); if (!f.exists()) { System.out.println("File 'example' does not exist!"); System.exit(1); Scanner infile = new Scanner(f); int sum = 0; while (infile.hasnextint()) { sum += infile.nextint(); System.out.println("Sum = " + sum); 11

1.3 Input Tokens (1/3) Input data are broken into tokens when read. Scanner view all input as a stream of characters, which it processes with its input cursor Each call to extract the next input (next(), nextint(), nextdouble(), etc.) advances the cursor to the end of the current token Tokens are separated by whitespace 12

1.3 Input Tokens (2/3) import java.util.*; InputTokens.java import java.io.*; public class InputTokens { public static void main(string[] args) throws FileNotFoundException { Scanner infile = new Scanner(new File("tokens")); int a = infile.nextint() String b = infile.next(); String c = infile.nextline(); double d = infile.nextdouble(); System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); File tokens : (viewed on screen) 123 CS1020 Data Structures and Algorithms 1 456 78.9 (internally) 123 CS1020 Data Structures and Algorithms 1\n456 78.9\n 13

1.3 Input Tokens (3/3) a = 123 b = CS1020 c = Data Structures and Algorithms 1 d = 456.0 int a = infile.nextint(); String b = infile.next(); String c = infile.nextline(); double d = infile.nextdouble(); System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); System.out.println("d = " + d); File tokens : 123 CS1020 Data Structures and Algorithms 1\n456 78.9\n After int a = infile.nextint(); 123 CS1020 Data Structures and Algorithms 1\n456 78.9\n After String b = infile.next(); 123 CS1020 Data Structures and Algorithms 1\n456 78.9\n After String c = infile.nextline(); 123 CS1020 Data Structures and Algorithms 1\n456 78.9\n After double d = infile.double(); 123 CS1020 Data Structures and Algorithms 1\n456 78.9\n 14

1.4 Tokenizing a String A Scanner can tokenize a string import java.util.*; import java.io.*; public class StringTokenize { public static void main(string[] args) { String msg = "345 students in CS1020."; Scanner sc = new Scanner(msg); StringTokenize.java int a = sc.nextint() String b = sc.next(); String c = sc.nextline(); System.out.println("a = " + a); System.out.println("b = " + b); System.out.println("c = " + c); 15

1.5 Exercise: Runners (1/4) Write a program to read in the distances run by a group of runners Sample input file runners_data : Runner ID (type int), name (String, a single word), followed by a list of distances in km (type double) You may assume that there are at least one runner and each runner has at least one distance record 123 Charlie 6.5 5.2 7.8 5.8 7.2 6.6 9.2 7.2 987 Alex 12.8 312 Jenny 5.7 4 6.2 509 Margaret 3.1 3.4 3.2 3.1 3.5 610 Richard 11.2 13.2 10.8 9.5 15.8 12.4 16

1.5 Exercise: Runners (2/4) import java.util.*; RunnersFlawed.java import java.io.*; public class RunnersFlawed { public static void main(string[] args) throws FileNotFoundException { Scanner infile = new Scanner(new File("runners_data")); int count = 0; double totaldist = 0.0; while (infile.hasnext()) { infile.nextint(); // read ID infile.next(); // read name while (infile.hasnextdouble()) { count++; totaldist += infile.nextdouble(); System.out.printf("Total distance = %.2f\n", totaldist); System.out.printf("Average distance per run = %.2f\n", totaldist/count); Exception in thread "main" java.util.inputmismatchexception at java.util.scanner.throwfor(scanner.java:864) at java.util.scanner.next(scanner.java:1485) at java.util.scanner.nextint(scanner.java:2117) at java.util.scanner.nextint(scanner.java:2076) at RunnersFlawed.main(RunnersFlawed.java:14) 17

1.5 Exercise: Runners (3/4) What went wrong? int count = 0; double totaldist = 0.0; while (infile.hasnext()) { infile.nextint(); // read ID infile.next(); // read name while (infile.hasnextdouble()) { count++; totaldist += infile.nextdouble(); RunnersFlawed.java 123 Charlie 6.5 5.2 7.8 5.8 7.2 6.6 9.2 7.2 987 Alex 12.8 312 Jenny 5.7 4 6.2 509 Margaret 3.1 3.4 3.2 3.1 3.5 610 Richard 11.2 13.2 10.8 9.5 15.8 12.4 18

1.5 Exercise: Runners (4/4) Solution: read line by line, then read tokens from each line. // Earlier portion omitted for brevity RunnersCorrected.java Scanner infile = new Scanner(new File("runners_data")); int count = 0; double totaldist = 0.0; while (infile.hasnextline()) { String line = infile.nextline(); Scanner sc = new Scanner(line); sc.nextint(); // read ID sc.next(); // read name while (sc.hasnextdouble()) { count++; totaldist += sc.nextdouble(); // Later portion omitted for brevity Total distance = 173.40 Average distance per run = 7.54 19

2 File Output

2.1 PrintStream (1/2) In java.io package PrintStream: An object that allows you to print output to a file Any methods you have used on System.out (such as println()) will work on a PrintStream PrintStream name = new PrintStream(new File("filename")); Example: PrintStream ps = new PrintStream(new File("greetings")); ps.println("hello world!"); ps.println("the quick brown fox jumps over the lazy dog."); Materials from Pearson 21

2.1 PrintStream (2/2) PrintStream name = new PrintStream(new File("filename")); If the file does not exist, it is created. If the file already exists, it is overwritten. Note: Do NOT open the same file for reading (Scanner) and writing (PrintStream) at the same time You will overwrite the input file with an empty file Materials from Pearson 22

2.2 System.out and PrintStream System.out is actually a PrintStream A reference to it can be stored in a PrintStream variable Printing to that variable causes console output to appear PrintStream out1 = System.out; PrintStream out2 = new PrintStream(new File("data.txt")); out1.println("hello, console!"); // goes to console out2.println("hello, file!"); // goes to file Materials from Pearson 23

2.3 Exercise: Runners (revisit) Modify RunnersCorrected.java to send its output to the file running_stat. import java.util.*; RunnersOutfile.java import java.io.*; public class RunnersOutfile { public static void main(string[] args) throws FileNotFoundException { Scanner infile = new Scanner(new File("runners_data")); // code omitted for brevity PrintStream outfile = new PrintStream(new File("running_stat")); outfile.printf("total distance = %.2f\n", totaldist); outfile.printf("average distance per run = %.2f\n", totaldist/count); outfile.close(); 24

3 Input and Output Streams

3.1 InputStream and OutputStream (1/2) InputStream and OutputStream are abstractions of the different ways to input and output data That is, it doesn t matter if the stream is a file, a web page, a video, etc. All that matters is that you receive information from the stream or send information into the stream. InputStream is an abstract superclass that provides a minimal programming interface and a partial implementation of input streams. It defines methods for reading bytes, arrays of bytes, etc. OutputStream is an abstract superclass that provides a minimal programming interface and a partial implementation of output streams. It defines methods for writing bytes or arrays of bytes to the stream. 26

3.1 InputStream and OutputStream (2/2) 27

3.2 Example: Using OutputStream We will use some of the methods in OutputStream below: 28

3.2 Example: Using OutputStream import java.io.*; public class TestOutputStream { TestOutputStream.java public static void main(string[] args) throws IOException { String msg = new String("Hello world!"); OutputStream out = new FileOutputStream("msg_file"); byte[] bytes = msg.getbytes(); out.write(bytes); out.write(bytes[1]); out.write(10); // ASCII value of newline out.write(bytes, 3, 5); out.close(); javac TestOutputStream.java java TestOutputStream cat msg_file Hello world!e lo wo 29

3.2 Example: Using InputStream 30

3.2 Example: Using InputStream import java.io.*; public class TestInputStream { TestInputStream.java public static void main(string[] args) throws IOException { InputStream in = new FileInputStream("msg_file")); int value; while ((value = in.read())!= -1) { System.out.print((char)value); System.out.println(); in.close(); javac TestInputStream.java java TestInputStream Hello world!e lo wo 31

End of file