Remedial Java - io 8/09/16. (remedial) Java. I/O. Anastasia Bezerianos 1

Similar documents
Java Input/Output. 11 April 2013 OSU CSE 1

Lecture 4: Exceptions. I/O

File IO. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 20

I/O in Java I/O streams vs. Reader/Writer. HW#3 due today Reading Assignment: Java tutorial on Basic I/O

Darshan Institute of Engineering & Technology for Diploma Studies

IT101. File Input and Output

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Byte and Character Streams. Reading and Writing Console input and output

I/O STREAM (REQUIRED IN THE FINAL)

Lecture 11.1 I/O Streams

Input from Files. Buffered Reader

CS 251 Intermediate Programming Java I/O Streams

Inheritance E, xc Ex eptions ceptions I/O

09-1. CSE 143 Java GREAT IDEAS IN COMPUTER SCIENCE. Overview. Data Representation. Representation of Primitive Java Types. Input and Output.

Object-Oriented Programming Design. Topic : Streams and Files

CS1092: Tutorial Sheet: No 3 Exceptions and Files. Tutor s Guide

Course Content. Objectives of Lecture 22 File Input/Output. Outline of Lecture 22. CMPUT 102: File Input/Output Dr. Osmar R.

File Input/Output. Introduction to Computer Science I. Overview (1): Overview (2): CSE 1020 Summer Bill Kapralos. Bill Kapralos.

PIC 20A Streams and I/O

Lesson 3: Accepting User Input and Using Different Methods for Output

COMP 213. Advanced Object-oriented Programming. Lecture 19. Input/Output

File Operations in Java. File handling in java enables to read data from and write data to files

Basic I/O - Stream. Java.io (stream based IO) Java.nio(Buffer and channel-based IO)

Java in 21 minutes. Hello world. hello world. exceptions. basic data types. constructors. classes & objects I/O. program structure.

Software 1 with Java. Recitation No. 7 (Java IO) May 29,

Object Oriented Software Design

Software 1 with Java. Recitation No. 9 (Java IO) December 10,

Object Oriented Software Design

Input & Output in Java. Standard I/O Exception Handling

Agenda & Reading. Python Vs Java. COMPSCI 230 S Software Construction

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

File. Long term storage of large amounts of data Persistent data exists after termination of program Files stored on secondary storage devices

Simple Java Input/Output

תוכנה 1 תרגול 8 קלט/פלט רובי בוים ומתי שמרת

TEXT-BASED APPLICATIONS

I/O Streams. Object-oriented programming

Software 1. Java I/O

Remedial Java - Excep0ons 3/09/17. (remedial) Java. Jars. Anastasia Bezerianos 1

ITI Introduction to Computer Science II

Today s plan Discuss the Lab 1 Show Lab 2 Review basic Java materials Java API Strings Java IO

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Au

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

CS Programming I: File Input / Output

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE. Representation of Primitive Java Types. CSE143 Sp

Today s plan Discuss the Bb quiz 1 Clarify Lab 1 Review basic Java materials Classes, Objects, Interfaces Strings Java IO. Understanding Board

Software Practice 1 - File I/O

Objec&ves. Review. Standard Error Streams

Overview CSE 143. Input and Output. Streams. Other Possible Kinds of Stream Converters. Stream after Stream... CSE143 Wi

HST 952. Computing for Biomedical Scientists Lecture 8

Overview CSE 143. Data Representation GREAT IDEAS IN COMPUTER SCIENCE

Any serious Java programmers should use the APIs to develop Java programs Best practices of using APIs

CS Programming I: File Input / Output

CSC 1214: Object-Oriented Programming

Chapter 5 Some useful classes

Today. Book-keeping. File I/O. Subscribe to sipb-iap-java-students. Inner classes. Debugging tools

CS193j, Stanford Handout #26. Files and Streams

Exceptions and Working with Files

Special error return Constructors do not have a return value What if method uses the full range of the return type?

7 Streams and files. Overview. Binary data vs text. Binary data vs text. Readers, writers, byte streams. input-output

Week 12. Streams and File I/O. Overview of Streams and File I/O Text File I/O

Lecture 22. Java Input/Output (I/O) Streams. Dr. Martin O Connor CA166

File Processing in Java

Question 1. (2 points) What is the difference between a stream and a file?

1. Download the JDK 6, from

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

Here is a hierarchy of classes to deal with Input and Output streams.

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Object-Oriented Programming in the Java language

1.00 Lecture 30. Sending information to a Java program

Java Input / Output. CSE 413, Autumn 2002 Programming Languages.

Example: Copying the contents of a file

WOSO Source Code (Java)

Classes Basic Overview

Each command-line argument is placed in the args array that is passed to the static main method as below :

COMP6700/2140 Input/Output

USING LIBRARY CLASSES

Variables of class Type. Week 8. Variables of class Type, Cont. A simple class:

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

CSE 143 Sp03 Midterm 2 Sample Solution Page 1 of 7. Question 1. (2 points) What is the difference between a stream and a file?

CISC 323 (Week 9) Design of a Weather Program & Java File I/O

Introduction Unit 4: Input, output and exceptions

23 Error Handling What happens when a method is called? 23.1 What is Exception Handling? A.m() B.n() C.p()

输 入输出相关类图. DataInput. DataOutput. java.lang.object. FileInputStream. FilterInputStream. FilterInputStream. FileOutputStream

Chapter 10. IO Streams

Programmierpraktikum

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

Objec&ves STANDARD ERROR. Standard Error Streams. Ø Byte Streams Ø Text Streams 10/5/16. Oct 5, 2016 Sprenkle - CSCI209 1

COMP 202 File Access. CONTENTS: I/O streams Reading and writing text files. COMP 202 File Access 1

Software 1 with Java. The java.io package. Streams. Streams. Streams. InputStreams

COMP200 INPUT/OUTPUT. OOP using Java, based on slides by Shayan Javed

Welcome to CIS 068! 1. GUIs: JAVA Swing 2. (Streams and Files we ll not cover this in this semester, just a review) CIS 068

Simple Java I/O. Part I General Principles

Chapter 12. File Input and Output. CS180-Recitation

Case Study: Savings Account Interest

Programming Languages and Techniques (CIS120)

C17: I/O Streams and File I/O

Data Types Reference Types

Chapter 4 Java I/O. X i a n g Z h a n g j a v a c o s q q. c o m

CS112 Lecture: Streams

C17: File I/O and Exception Handling

Transcription:

(remedial) Java anastasia.bezerianos@lri.fr I/O Anastasia Bezerianos 1

Input/Output Input Output Program We ve seen output System.out.println( some string ); Anastasia Bezerianos 2

Standard input/output! Standard output (console) System.out.println( ) System.err.println( )! Standard input (keyboard) InputStreamReader cin = new InputStreamReader(System.in); //imports you ll need? public class simpleio { An example of input public static void main (String[] args){ System.out.print("Give me some input: "); InputStreamReader stream = new InputStreamReader(System.in); BufferedReader keybordinput = new BufferedReader (stream); //this will almost work, deal with i/o exceptions input = keybordinput.readline(); System.out.println("Your input was: " + input); Anastasia Bezerianos 3

The full picture Disk network 10001110100111010101011 InputStream e.g., System.In s, o, m, e,, i, n, p, u, t, InputStreamReader "some input\n" BufferedReader InputStream! InputStream is a stream of bytes! Read one byte after another using read()! A byte is just a number! Data on your hard drive is stored in bytes! Bytes can be interpreted as characters, numbers.. InputStream stream = System.in; Anastasia Bezerianos 4

InputStreamReader! Reader is a class for reading character streams! Read one character after another using read()! InputStreamReader takes an InputStream and converts bytes to characters! Still inconvenient! Can only read a character at a time.. new InputStreamReader(stream); BufferedReader! BufferedReader buffers a character stream so that you can read it line by line! Read line using readline() new BufferedReader ( new InputStreamReader(System.in)); Anastasia Bezerianos 5

User input InputStreamReader stream = new InputStreamReader(System.in); BufferedReader keyboardinput = new BufferedReader (stream); String answer = keyboardinput.readline(); Exercise 3 part one www.lri.fr/~anab/teaching/remedialjava/ex- class4.pdf Reminder: InputStreamReader stream = new InputStreamReader(System.in); BufferedReader keyboardinput = new BufferedReader (stream); String answer = keyboardinput.readline();! You can make a string all lower case by calling on a String object: str.tolowercase() Anastasia Bezerianos 6

User input! BufferedReader and other streams have mul_ple reading func_ons. E.g.,:! readline()! read(), reads an integer Aside: string manipula_on! When reading a line from StdIn (or from a file), it is returned as a string. There are usefull methods for manipulating strings you receive as input:! split strings when a character (e.g.,, ) is found, and put the pieces into an array of strings String[] split = str.split(, );! trim empty spaces in beginning/end of string String trimmed = str.trim(); Anastasia Bezerianos 7

Aside: string manipula_on (cont d)! Others, that are usefull when testing the content of the string (self explanatory) startswith(s1), endswith(s1), substring(s1) touppercase(), tolowercase(), equals(s1), isempty(), length() Aside: string manipula_on (cont d)! Finally, there are methods to convert a string to another type Integer.parseInt( str ) ; // e.g., int i = Integer.parseInt( 100 ) ; double mydouble = Double.parseDouble(str); // e.g., double d = Double.parseDouble( 2.5 ); boolen mybool = Boolean.parseBoolean(str); // e.g., boolean b = Boolean.parseBoolean( true ); Anastasia Bezerianos 8

Exercise 4 www.lri.fr/~anab/teaching/remedial- java/ex- class4.pdf Reminder:! Some conversion methods int i = Integer.parseInt( str ) ; double d = Double.parseDouble (str);! Some String manipula_on methods String[] split = str.split(, ); String trimmed = str.trim(); Exercise 5 www.lri.fr/~anab/teaching/remedial- java/ex- class4.pdf Reminder:! Some conversion methods int i = Integer.parseInt( str ) ; double d = Double.parseDouble (str);! Some String manipula_on methods String[] split = str.split(, ); String trimmed = str.trim(); Anastasia Bezerianos 9

FileReader! FileReader takes a text file! converts it into a character stream! FileReader( PATH TO FILE );! Use this and BufferReader to read files FileReader fr = new FileReader( readme.txt ); BufferedReader br = new BufferedReader (r); An example of reading a file //imports you ll need? public class simplereadfile { public static void main (String[] args) throws IOException{ //Eclipse quirk: path names are relative to project dir FileReader fr = new FileReader( readme.txt ); BufferedReader bf = new BufferedReader (fr); String line = null; while( (line = bf.readline())!= null ) { System.out.println(line); bf.close(); Anastasia Bezerianos 10

Exercise 6 www.lri.fr/~anab/teaching/remedialjava/ex- class4.pdf Reminder: FileReader fr = new FileReader( readme.txt ); BufferedReader bf = new BufferedReader (fr); String line = null; while( (line = bf.readline())!= null ) System.out.println(line); bf.close(); Input/Output goals! The fundamental concept is that of a stream! the VM connects to, or creates a data stream Anastasia Bezerianos 11

Input/Output goals Input/Output (I/O) is the way a program communicates with it s environment:! Where does the I/O come from?! disk file, console, network! How is it formajed?! binary, byte, character! per line, per word, per character! sequen_ally, random access! buffer Input/Output stream Input: e.g. read a file Program is the destination Output: e.g. write in a file Program is the origin, file the destination Anastasia Bezerianos 12

Input/Output goals! The package java.io regroups classes for I/O! It is divided in two categories:! input and output! and for each, 2 abstract classes, one that handles bytes, one that handles characters java.io (input) The input classes have the methods read() or read(x)! InputStream with method: public int read( byte[] b ) throws IOException! Reader with method: public int read( char[] c ) throws IOException Always close stream with close() Anastasia Bezerianos 13

java.io (input)! The BufferedReader is a wrapper class that coverts an input stream to Strings:! Reader with method: public String readline() throws IOException Always close stream with close() java.io (output) The output classes have the method write(x)! OuputStream with method: public void write( byte[] b ) throws IOException! Writer with methods: public void write( char[] c ) throws IOException append() Always close stream with close() Anastasia Bezerianos 14

java.io (input)! Similarly, BufferedWriter is a wrapper class that coverts an output stream to String:! Writer with method: public String write(string) throws IOException Always close stream with close() An example of wri_ng to a file //imports you ll need? public class simplewritefile { public static void main (String[] args) throws IOException{ //Eclipse quirk: path names are relative to project dir String[] mytext = { this, is, my, text ; BufferedWriter bf = new BufferedWriter ( new FileWriter( myfile.txt )); for( int i =0; i < mytext.length; ++i) { bf.write(mytext[i] + \n ); bf.close(); Anastasia Bezerianos 15

An example of wri_ng to a file //imports you ll need? public class simplewritefile { public static void main (String[] args) throws IOException{ //Eclipse quirk: path names are relative to project dir String[] mytext = { this, is, my, text ; BufferedWriter bf = new BufferedWriter ( new FileWriter( myfile.txt )); DataReader out = new DataReader (bf); for( int i =0; i < mytext.length; ++i) { out.write(mytext(i)); bf.close(); public class CopyCharacters { A read/write example public static void main(string[] args) throws IOException { FileReader input = null; // input stream as characters FileWriter output = null; // output stream as characters input = new FileReader("original.txt"); // open file output = new FileWriter("copie.txt"); // open file int c; // represents a byte while ((c = input.read())!= -1){ output.write(c); // read one by one // chars until the end // c as int if (input!= null) { input.close(); // close the input stream if (output!= null) { output.close(); // close the output stream Anastasia Bezerianos 16

Aside: from String to basic types! Usefull methods for manipula_ng input:! Beyond the String class, Java has the classes Integer, Double, Float, etc. that contain usefull methods for manipula_ng the corresponding basic types.! Some example that convert them to strings: String s = Integer.toString(10); String s = Double.toString(10.0);! Some example that convert Strings to basic types: int i = String.toInt( 10 ); double d = String.toDouble( 10.0 ); Exercise 7 www.lri.fr/~anab/teaching/remedialjava/ex- class45.pdf Reminder: BufferedWriter bf = new BufferedWriter ( new FileWriter( myfile.txt )); bf.write( str ); bf.close(); Anastasia Bezerianos 17

The class File File! The class java.io.file helps with file manipula_on independent of plaform! A bejer name could be FileName or FilePath, as it represents the name of a file or files in a directory! Interes_ng methods getabsolutepath() exists() isdirectory() listfiles() list (FilenameFilter ff) Anastasia Bezerianos 18