Lab 8 - Input and Output

Similar documents
INPUT AND OUTPUT STREAMS

What is an I/O Stream?

FILE I/O IN JAVA. Prof. Chris Jermaine Prof. Scott Rixner

D06 PROGRAMMING with JAVA

Object-Oriented Programming in Java

JAVA - FILES AND I/O

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

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

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

Programming in Java

Files and input/output streams

Using Files as Input/Output in Java 5.0 Applications

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

Creating a Simple, Multithreaded Chat System with Java

Building a Multi-Threaded Web Server

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

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

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

Simple Java I/O. Streams

WRITING DATA TO A BINARY FILE

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

Basics of Java Programming Input and the Scanner class

Chapter 2: Elements of Java

13 File Output and Input

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

Lesson: All About Sockets

Input / Output Framework java.io Framework

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

Stream Classes and File I/O

Introduction to Java

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

CS 1302 Ch 19, Binary I/O

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

Chapter 3. Input and output. 3.1 The System class

CS506 Web Design and Development Solved Online Quiz No. 01

Event-Driven Programming

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

Introduction to Programming

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Install Java Development Kit (JDK) 1.8

Division of Informatics, University of Edinburgh

Reading Input From A File

Principles of Software Construction: Objects, Design, and Concurrency. Design Case Study: Stream I/O Some answers. Charlie Garrod Jonathan Aldrich

Java from a C perspective. Plan

6.1. Example: A Tip Calculator 6-1

TP N 10 : Gestion des fichiers Langage JAVA

Assignment 4 Solutions

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

Lecture 5: Java Fundamentals III

Introduction to Java. CS 3: Computer Programming in Java

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

1 of 1 24/05/ :23 AM

B.Sc (Honours) - Software Development

Sample CSE8A midterm Multiple Choice (circle one)

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

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

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

Programming Languages CIS 443

Comp 248 Introduction to Programming

Readings and References. Topic #10: Java Input / Output. "Streams" are the basic I/O objects. Input & Output. Streams. The stream model A-1.

Question R11.3. R11.3 How do you open a file whose name contains a backslash, like c:\temp\output.dat?

Some Scanner Class Methods

Course Intro Instructor Intro Java Intro, Continued

Visit us at

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

CS 106 Introduction to Computer Science I

AP Computer Science Java Mr. Clausen Program 9A, 9B

AVRO - SERIALIZATION

D06 PROGRAMMING with JAVA

Chapter 2 Introduction to Java programming

Amazon EC2 KAIST Haejoon LEE

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

CSE 8B Midterm Fall 2015

Programming Fundamentals I CS 110, Central Washington University. November 2015

Quick Introduction to Java

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

An Overview of Java. overview-1

java Features Version April 19, 2013 by Thorsten Kracht

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivepacket.getaddress(); int port = receivepacket.

COSC 6397 Big Data Analytics. Mahout and 3 rd homework assignment. Edgar Gabriel Spring Mahout

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

Topic 11 Scanner object, conditional execution

Building Java Programs

Lecture J - Exceptions

Java Interview Questions and Answers

Arrays in Java. Working with Arrays

CS 106 Introduction to Computer Science I

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

Evaluation. Copy. Evaluation Copy. Chapter 7: Using JDBC with Spring. 1) A Simpler Approach ) The JdbcTemplate. Class...

Building Java Programs

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

Software Development in Java

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

Part I. Multiple Choice Questions (2 points each):

Using NetBeans IDE to Build Quick UI s Ray Hylock, GISo Tutorial 3/8/2011

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

JAVA.UTIL.SCANNER CLASS

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Transcription:

Lab 8 - Input and Output David Woods dwoods@scss.tcd.ie 1 Streams Input and Output usually abbreviated as I/O are how we handle files in Java and other programming languages. An I/O stream is what Java uses to get data from an input source, or to put data into an output destination. Streams allow us to handle many kinds of data, including the simple primitive types (int, boolean, char, double, etc.) we ve seen already, and more complex objects (String, Scanner, List, etc.). Sometimes streams will just move data from one place to another, and sometimes they will process it in some useful way before passing it on. No matter what the stream does, it will always follow the same basic model: Reading information into a program Data Source Input Stream Program Writing information from a program Program Output Stream Data Source We ve actually already seen an example of an input stream, when we were using the Scanner class. This takes data from an input stream, which in this case is System.in, which corresponds to the keyboard input (this is also known as standard in). The Scanner allows us to access the information from the input stream in our programs (using next(), nextint(), nextline(), etc.). Similarly, System.out is an output stream, corresponding to the output on the screen (standard out). java.io In order to access the classes discussed below, we will need to import them into our programs. We do this in the same way as we import the Scanner class import java.util.scanner except that all the classes we want are inside the java.io library. We can either import the classes we want one by one, or we can use the asterisk character to import everything from the library: import java.io.* One important thing to be aware of with data streams is that things can go wrong. A file can go missing, or a stream can be interrupted (if the program is ended early, for example). We need to be able to deal with these exceptions, and we have two ways to do this: 1

Method 1: try / catch We can wrap all of our code in a try block, which will try to do the tasks we want, but in the case where something goes wrong, we can catch the exception (which for these cases will be of the type IOException), and handle it in a catch block. try // code using the stream catch (IOException e) // code to only be executed if the exception occurs Method 2: throws Exception Instead of wrapping our code up, we can anticipate that function might contain some code that could run into one of the exceptional circumstances discussed above. After the function definition and arguments, we add the keyword throws, followed by the kind of exception that we are expecting. public static void main(string[] args) throws IOException // code using the stream Note that this seems like we re avoiding the problem of handling the exception (which we did with the catch block in the previous method) in fact, we re just moving the problem, and we ll have to worry about it elsewhere. For now, though, we don t need to worry about that too much. 2 Byte Streams In the same way that, ultimately, we can break down every data type to ones and zeroes, grouped in 8-bit bytes, every type of stream is ultimately a descendant of a byte stream either InputStream or OutputStream. On their own, these aren t too useful to us, as they are very low-level, and we don t usually want to move just bytes around, but it s important to understand what s going on behind the scenes. For example, using the classes FileInputStream and FileOutputStream, we could get a stream of bytes from one file, process it in some useful way, and then write it to some other file. However, generally speaking we will not use byte streams unless we need access to this low-level data for some reason. Since we will be mostly dealing with text data as computational linguists, we will typically use character streams to handle moving our data around. Stream Pipeline You should always deal with streams in the following order: 2

1. open the stream 2. process the data 3. close the stream It s important to always close your streams when you no longer need them, as doing otherwise may lead to resource and memory leaks. These can cause your computer to slow down, and corrupted files in bad cases. This goes for Scanners too, as closing one closes the input stream it s handling (System.in), as in the example below: public static void main(string[] args) // open a Scanner stream, using the keyboard as the data source Scanner scan = new Scanner(System.in); System.out.println("Enter some text: "); // get the data from the stream String str = scan.nextline(); // process the data in some way str = str.tolowercase(); System.out.println(str); // close the stream, preventing memory leaks! scan.close(); 3 Character Streams The simplest character stream classes are called Reader and Writer, but as with the byte streams, we have specialised versions of these classes for use in file I/O, which is what we are most interested in at the moment: FileReader and FileWriter. These classes allow us to pass a filename as a String argument, and open up that file to access the characters of the text inside it, or write characters to a file. Note that the input stream s file must exist before trying to open it, but if the output stream s file does not exist, it will be created automatically. import java.io.filereader; import java.io.filewriter; import java.io.ioexception; public class CopyChars // main function can throw IOException public static void main(string[] args) throws IOException // open the streams FileReader inputstream = new FileReader("myinput.txt"); FileWriter outputstream = new FileWriter("myoutput.txt"); // grab the first character int c = inputstream.read(); // a result of -1 means the end of the stream is reached while (c!= -1) 3

// write the character into the output file outputstream.write(c); // grab the next character c = inputstream.read(); // close the streams! inputstream.close(); outputstream.close(); You ll notice that that this uses an integer to store the values of the characters this is just due to the way that the streams represent the characters internally. Getting Lines of Data All of the above is fine for working in low-level data situations, but we normally want to deal with more than one byte or one character at a time. Typically, we re going to be dealing with Strings of text. The most straightforward way of building on what we already know is to take units of lines, rather than single characters. A line is a String of characters with some line-terminator at the end. What this terminator is will depend on the system you re using, but is usually one of a carriage-return/line-feed sequence ("\r\n"), or a single carriage-return ("\r"), or a single line-feed ("\n"). We don t need to worry about which is being used most of the time, as Java generally can figure it out without our help, but sometimes if things aren t looking right, it may be due to a mismatched lineterminator. So how do we get a line of characters using I/O streams? We need to introduce another pair of classes to do this for us: BufferedReader and PrintWriter. These classes will allow us to create buffers of characters to deal with in the format of Strings. We use the classes in conjunction with the two classes we used for simple character streams, FileReader and FileWriter. 4

import java.io.filereader; import java.io.filewriter; import java.io.bufferedreader; import java.io.printwriter; import java.io.ioexception; public class CopyLines // main function can throw IOException public static void main(string[] args) throws IOException // open the streams FileReader inputstream = new FileReader("myinput.txt"); FileWriter outputstream = new FileWriter("myoutput.txt"); // pass the basic streams to the new buffered streams BufferedReader reader = new BufferedReader(inputStream); PrintWriter writer = new PrintWriter(outputStream); // grab the first line String line = reader.readline(); // a result of null means the end of the stream is reached while (line!= null) // write the line into the output file writer.println(line); // grab the next character line = reader.readline(); // close the streams! inputstream.close(); outputstream.close(); reader.close(); writer.close(); You should also be aware that it is possible to initialise the buffered streams in a single line each: BufferedReader br = new BufferedReader(new FileReader("input.txt")); Doing it this way means you only have to close the buffered stream, and it will close the character stream too: // closes the buffered and simple streams at once br.close(); 5

4 Lab Exercise Please sumbit your solution to me by email (dwoods@scss.tcd.ie) before 23:59 on Monday 21st of November. Ensure all code is fully commented and tested, or full marks will not be given. Remember that you must recompile your code every time you make any changes. Create a short text file (approximately 10 lines). Create a Java program called SimpleIO.java The program should ask the user for an input file (the text file you created in the first step), and should make sure this file exists and is not a directory (you ll have to look online for these tests). It should keep asking the user for input until an existant file is given. Next, the program should ask for another file name, for output. This file does not need to exist, but it must be a distinct name from the input file. Keep asking until this condition is satisfied. Read through the input file, line by line, and count the occurrences of the word the in each line. Hint: split the string into an array of lowercase words. For each line of input, you should print the line in the output file, followed by the number of occurrences of the. For example, if the input line is The cat and the dog, the output line should be The cat and the dog [2] 6