Stream Manipulation. Lecture 11

Similar documents
Mobile Applications Grzegorz Budzyń Lecture. 1: Introduction JAVA

Object-Oriented Programming Design. Topic : Streams and Files

Data Structures. 03 Streams & File I/O

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

Chapter 10. IO Streams

1.00 Lecture 30. Sending information to a Java program

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

Lecture 7. File Processing

Darshan Institute of Engineering & Technology for Diploma Studies

CS 251 Intermediate Programming Java I/O Streams

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

Title Description Participants Textbook

5/29/2006. Announcements. Last Time. Today. Text File I/O Sample Programs. The File Class. Without using FileReader. Reviewed method overloading.

Software Practice 1 - File I/O

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

What is Serialization?

Streams and File I/O

Chapter 10 Input Output Streams

Chapter 12. File Input and Output. CS180-Recitation

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

User Data Protocol (2)

Java Input/Output Streams

I/O Streams. Object-oriented programming

CN208 Introduction to Computer Programming

Java Programming Lecture 9

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

HST 952. Computing for Biomedical Scientists Lecture 8

AppFileIO > PrFileIO> ByteDataStreamIO import java.io.*; in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));

Performing input and output operations using a Byte Stream

School of Informatics, University of Edinburgh

Software 1. Java I/O

//Initializes the variables that will hold the eventual final new versions of the string String pluralextension; String secondwordextension;

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

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

12% of course grade. CSCI 201L Final - Written Fall /7

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

1.00 Lecture 31. Streams 2. Reading for next time: Big Java , The 3 Flavors of Streams

Input from Files. Buffered Reader

ANNA UNIVERSITY OF TECHNOLOGY COIMBATORE B.E./B.TECH DEGREE EXAMINATIONS: NOV/DEC JAVA PROGRAMMING

Exceptions and Working with Files

Java Input/Output. 11 April 2013 OSU CSE 1

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

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

Chapter 10. File I/O. Copyright 2016 Pearson Inc. All rights reserved.

Recitation: Loop Jul 7, 2008

Active Learning: Streams

The Java Series IO, Serialization and Persistence. The Java Series. IO, Serialization and Persistence Raul RAMOS / CERN-IT User Support Slide 1

Chapter 17 Binary I/O. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2017 Pearson Education, Inc. All rights reserved.

CMPSCI 187: Programming With Data Structures. Lecture #24: Files and a Case Study David Mix Barrington 2 November 2012

Files and IO, Streams. JAVA Standard Edition

Java Networking (sockets)

Principles, Models, and Applications for Distributed Systems M

COMP1406 Tutorial 11

IT101. File Input and Output

Example: Copying the contents of a file

Object-Oriented Programming in the Java language

CS Week 11. Jim Williams, PhD

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

Homework 3 Huffman Coding. Due Thursday October 11

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

School of Informatics, University of Edinburgh

Il linguaggio Java Programmi d esempio. Gli stream

Java file manipulations

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

Software Practice 1 - Socket

pre-emptive non pre-emptive

Java Programs. System.out.println("Dollar to rupee converion,enter dollar:");

Principles, Models, and Applications for Distributed Systems M

Optional Lecture Chapter 17 Binary IO

Files and Streams

Chapter 11: Exceptions and Advanced File I/O

Java Socket Application. Distributed Systems IT332

Chapter 12. File Input and Output. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Simple Java I/O. Part I General Principles

Chapter 12: Exceptions and Advanced File I/O

Programmierpraktikum

Web Server Project. Tom Kelliher, CS points, due May 4, 2011

CPSC 319. Week 2 Java Basics. Xiaoyang Liu & Sorting Algorithms

14.3 Handling files as binary files

LARA TECHNOLOGIES EXAM_SPECIALSIX SECTION-1

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

Various useful classes

System resources. Security Manager.

Writing usable APIs in practice

Simple Java Input/Output

CPIT 305 Advanced Programming

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

CSPP : Introduction to Object-Oriented Programming

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

Network. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

CS 200 File Input and Output Jim Williams, PhD

Events and Exceptions

Compile error. 2. A run time error occurs when the program runs and is caused by a number of reasons, such as dividing by zero.

Announcement EXAM 2. Assignment 6 due Wednesday WEDNESDAY 7: :00 PM EE 129

Input, Output, and Networks

Exceptions Binary files Sequential/Random access Serializing objects

PIC 20A Streams and I/O

Software 1. תרגול 9 Java I/O

File Input and Output Recitation 04/03/2009. CS 180 Department of Computer Science, Purdue University

Java Programming Unit 9. Working with I/O Streams. Java Serializa<on. Basic Networking.

Transcription:

Stream Manipulation Lecture 11

Streams and I/O basic classes for file IO FileInputStream, for reading from a file FileOutputStream, for writing to a file Example: Open a file "myfile.txt" for reading FileInputStream fis = new FileInputStream("myfile.txt"); Open a file "outfile.txt" for writing FileOutputStream fos = new FileOutputStream ("myfile.txt"); 2

Display File Contents import java.io.*; public class FileToOut1 { public static void main(string args[]) { try { FileInputStream infile = new FileInputStream("testfile.txt"); byte buffer[] = new byte[50]; int nbytesread; do { nbytesread = infile.read(buffer); System.out.write(buffer, 0, nbytesread); while (nbytesread == buffer.length); catch (FileNotFoundException e) { System.err.println("File not found"); catch (IOException e) { System.err.println("Read failed"); 3

Filters Once a stream (e.g., file) has been opened, we can attach filters Filters make reading/writing more efficient Most popular filters: For basic types: DataInputStream, DataOutputStream For objects: ObjectInputStream, ObjectOutputStream 4

Writing data to a file using Filters import java.io.*; public class GenerateData { public static void main(string args[]) { try { FileOutputStream fos = new FileOutputStream("stuff.dat"); DataOutputStream dos = new DataOutputStream(fos); dos.writeint(2); dos.writedouble(2.7182818284590451); dos.writedouble(3.1415926535); dos.close(); fos.close(); catch (FileNotFoundException e) { System.err.println("File not found"); catch (IOException e) { System.err.println("Read or write failed"); 5

Reading data from a file using filters import java.io.*; public class ReadData { public static void main(string args[]) { try { FileInputStream fis = new FileInputStream("stuff.dat"); DataInputStream dis = new DataInputStream(fis); int n = dis.readint(); System.out.println(n); for( int i = 0; i < n; i++ ) { System.out.println(dis.readDouble()); dis.close(); fis.close(); catch (FileNotFoundException e) { System.err.println("File not found"); catch (IOException e) { System.err.println("Read or write failed"); 6

Using DataInputStream In many applications, it may be required to read in an entire line of text at a time. For this purpose, the DataInputStream class and its readline method can be used. A readline() reads in a line of ASCII text and converts it to a Unicode string Example : DataInputStream inp = new DataInputStream(new FileInputStream( Student.dat ) );... String line = inp.readline(); 7

Object serialization Serialization is the process of writing the state of an object to a byte stream. Write objects to a file, instead of writing primitive types. Use the ObjectInputStream, ObjectOutputStream classes, the same way that filters are used. 8

Write an object to a file import java.io.*; import java.util.*; public class WriteDate { public WriteDate () { Date d = new Date(); try { FileOutputStream f = new FileOutputStream("date.ser"); ObjectOutputStream s = new ObjectOutputStream (f); s.writeobject (d); s.close (); catch (IOException e) { e.printstacktrace(); public static void main (String args[]) { new WriteDate (); 9

Read an object from a file import java.io.*; import java.util.*; public class ReadDate { public ReadDate () { Date d = null; ObjectInputStream s = null; try { FileInputStream f = new FileInputStream ("date.ser"); s = new ObjectInputStream (f); catch (IOException e) { e.printstacktrace(); try { d = (Date)s.readObject (); catch (ClassNotFoundException e) { e.printstacktrace(); catch (InvalidClassException e) { e.printstacktrace(); catch (StreamCorruptedException e) { e.printstacktrace(); catch (OptionalDataException e) { e.printstacktrace(); catch (IOException e) { e.printstacktrace(); System.out.println ("Date serialized at: "+ d); public static void main (String args[]) { new ReadDate (); 10

Utility : StringTokenizer Parsing an input string. i.e. division of text into a set of discrete parts or tokens, which can convey a semantic meaning.

Utility : StringTokenizer import java.util.stringtokenizer; class STDemo { static String str = "title = Java : class;" + "instructor = A.B.C.;" + "time = 8.15 A.M.;" + "Date = 30/03/2007"; public static void main(string args[]) { StringTokenizer st = new StringTokenizer(str, "=;"); while(st.hasmoretokens()) { String key = st.nexttoken(); String val = st.nexttoken(); System.out.println(key + "\t" + val);