פיתוח מונחה עצמים – שפת JAVA קבצים. References קורס "שיטות בהנדסת תוכנה", הפקולטה למדעי המחשב, הטכניון. קורס "מערכות מידע מבוזרות", הפקולטה להנדסת תעשייה.

Slides:



Advertisements
Similar presentations
1 Streams and Input/Output Files Part 2. 2 Files and Exceptions When creating files and performing I/O operations on them, the systems generates errors.
Advertisements

Introduction to Java 2 Programming Lecture 7 IO, Files, and URLs.
Lecture 15: I/O and Parsing
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Streams Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
Standard input, output and error. Lecture Under Construction.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
James Tam Simple file handling in Java Simple File Input And Output Types of Java files Simple file output in Java Simple file input in Java.
Java I/O and Java Networking (Client Side) Yoshi.
Overview of Java I/O. Stream A Program often needs to read/write information from/to outer source/destination. Outer source/destination can be : –A File.
1 Text File I/O  I/O streams  Opening a text file for reading  Closing a stream  Reading a text file  Writing and appending to a text file.
James Tam Simple File Input And Output Types of Java Files Simple File Output in Java Simple File Input in Java.
1 Tirgul no. 13 Topics covered: H String parsing. H Text file I/O. H Extending Filters.
Tirgul 1 Today’s subject - Java reminders and additions: –Inner classes –Packages –I/O streams –Command Line Arguments –Primitive and Reference Data Types.
Chapter 91 Streams and File I/O CS-180 Recitation-03/07/2008.
Java I/O – what does it include? Command line user interface –Initial arguments to main program –System.in and System.out GUI Hardware –Disk drives ->
Java Review 2. The Agenda The following topics were highlighted to me as issues: –File IO (Rem) –Wrappers (Rem) –Interfaces (Rem) –Asymptotic Notation.
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
Exceptions and IO Dr. Andrew Wallace PhD BEng(hons) EurIng
Java I/O Input: information brought to program from an external source
Java Programming: I/O1 Java I/O Reference: java.sun.com/docs/books/tutorial/essential/io/
Lecture 7 Exceptions and I/O. Overview of the exception hierarchy A simplified diagram of the exception hierarchy in Java Throwable ErrorException IOException.
CIS 068 JAVA I/O: Streams and Files. CIS 068 I/O Usual Purpose: storing data to ‘nonvolatile‘ devices, e.g. harddisk Classes provided by package java.io.
Streams and Files CMPS Overview Stream classes File objects File operations with streams Examples in C++ and Java 2.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
Very Brief Introduction to Java I/O with Buffered Reader and Buffered Writer.
Prepared by : A.Alzubair Hassan Kassala university Dept. Computer Science Lecture 2 I/O Streams 1.
Chapter 9 1 Chapter 9 – Part 1 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.
Based on OOP with Java, by David J. Barnes Input-Output1 The java.io Package 4 Text files Reader and Writer classes 4 Byte stream files InputStream, FileInputStream,
Richiami di Java Input/Output. import java.io.*; public class Simple { public static void main(String a[]){ new Simple(); } public Simple() { byte buffer[]=new.
I / O in java. java.io BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter ByteArrayInputStream ByteArrayOutputStream CharArrayReader.
Java Input/Output CSE301 University of Sunderland Harry Erwin, PhD Half Lecture.
File IO Basics By Dan Fleck Coming up: Data Streams.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
1 Software 1 Java I/O. 2 The java.io package The java.io package provides: Classes for reading input Classes for writing output Classes for manipulating.
Lecture 5 I/O and Parsing
– Advanced Programming P ROGRAMMING IN Lecture 22 Input and Output System.
CSI 3125, Preliminaries, page 1 Java I/O. CSI 3125, Preliminaries, page 2 Java I/O Java I/O (Input and Output) is used to process the input and produce.
I/O Basics 26 January Aside from print( ) and println( ), none of the I/O methods have been used significantly. The reason is simple: most real.
Chapter - 11 Introduction to File and Streams This chapter includes -  Defining a File  Testing and Checking File Objects  Accessing File Objects.
DEPARTMENT OF COMPUTER SCIENCE N.HARIKA. Contents Overview of I/O Streams Character Streams Byte Streams Using the Streams 2.
Exception Handling, Reading and Writing in Files, Serialization, Exceptions, Files, Streams, File Readers and Writers, Serializable SoftUni Team Technical.
Java Input/Output. Java Input/output Input is any information that is needed by your program to complete its execution. Output is any information that.
CS 116 Object Oriented Programming II Lecture 11 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
The Java IO System Different kinds of IO Different kinds of operations
Keerthi Nelaturu Url: site.uottawa.ca/~knela006
Java Text I/O CS140 Dick Steflik. Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader.
Input and Output Streams – part I
Exception Handling, Reading and Writing in Files, Serialization,
OO Design and Programming II I/O: Reading and Writing
Lesson 8: More File I/O February 5, 2008
12: The Java I/O System stream model.
I/O Basics.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
JAVA IO.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Reading and Writing Text Files
Input and Output Stream
Files and Streams in Java
Web Design & Development Lecture 8
ECE 122 April 14, 2005.
EEC 484/584 Computer Networks
Java IO Packages Prepared by Mrs.S.Amudha AP/SWE
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
David Davenport Spring 2005
Presentation transcript:

פיתוח מונחה עצמים – שפת JAVA קבצים

References קורס "שיטות בהנדסת תוכנה", הפקולטה למדעי המחשב, הטכניון. קורס "מערכות מידע מבוזרות", הפקולטה להנדסת תעשייה וניהול, הטכניון.

I/O Streams in Java Introduction - 1 Definition Stream is a flow of Data. characters read from a file bytes written to the network …  The information can be read/written sequentially from/to the stream.  The assumption made on the stream: the information passes in FIFO order.

Source Program Stream reads Dest. Program Stream writes  No matter where the data is coming from or going to and no matter what its type, the algorithms for sequentially reading and writing data are basically the same: Introduction - 2 Reading: open a stream while more information read information close the stream Writing: open a stream while more information write information close the stream

Standard Input / Output Streams in Java Writing to the screen: System.out.print(…); System.out.println(…); Reading from the screen: System.in.read(byte[]); System.in.read(byte[],int,int); Standard streams are automatically opened, so no need to open them explicitly. Writing System.out.print(“Hello\n”); is the same as writing System.out.println(“Hello”); You have to create byte[] array before calling read function. read function has to be surrounded by try{ }catch(..){ } block.

Example - Echo package test; import java.io.*; public class IOTest { public IOTest() { } public static void main(String[] args) { byte[] ch = new byte[80]; int num = 0; try { num = System.in.read(ch); System.out.println(new String(ch,0,num)); // String(byte[] bt, int offset,int length) } catch (IOException ex) { System.out.println("Can't read"); ex.printStackTrace(); }

java.io Classes Hierarchy The Streams can be divided into Character Streams and Byte Streams Character Streams: Reader and Writer are the abstract superclasses for character streams in java.io. Reader provides the API and partial implementation for readers-- streams that read 16-bit characters--and Writer provides the API and partial implementation for writers--streams that write 16-bit characters. Byte Streams: To read and write 8-bit bytes, programs should use the byte streams, descendants of InputStream and OutputStream. InputStream and OutputStream provide the API and partial implementation for input streams (streams that read 8-bit bytes) and output streams (streams that write 8-bit bytes). These streams are typically used to read and write binary data such as images and sounds.

Character Streams Hierarchy Writer BufferedWriter CharArrayWriter StringWriter FilterWriter OutputStreamWriter PipedWriter FileWriter Reader BufferedReader CharArrayReader StringReader FilterReader InputStreamReader PipedReader PushbackReader FileReader LineNumberReader

Example import java.io.*; //must be imported public class Copy { public static void main(String[] args) throws IOException { File inputFile = new File("farrago.txt"); File outputFile = new File("outagain.txt"); //FileOutputStream and FileInputStream for bytes //throws FileNotFoundException (descendent of IOException) FileReader in = new FileReader(inputFile ); FileWriter out = new FileWriter(outputFile); int c; while ((c = in.read()) != -1) out.write(c); // housekeeping in.close(); out.close(); }

Advanced Usage //extracts from a class – not a whole class //args import java.io.*; import java.util.StringTokenizer;... try { int lineNum, wordNum; String line; BufferedReader inStream = new BufferedReader(new FileReader(args[0])); lineNum = wordNum = 0; do { line = inStream.readLine(); if(line != null) { lineNum++; StringTokenizer st = new StringTokenizer(line,args[1]); wordNum += st.countTokens(); } }while(line != null); System.out.println("There are " + lineNum + " lines."); System.out.println("There are " + wordNum + " words."); }catch(FileNotFoundException fnfe) { System.out.println("File ("+ args[0] +") not found."); }catch(IOException ioe) { System.out.println("I/O error while reading file ("+ args[0] +")"); }

private void writeToFile(String from, String to){ try{ FileReader fr = new FileReader(from); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter(to); String line = null, srt[]; String wline; int lineNum = wordNum = 0; while ( (line = br.readLine()) != null ) { str = line.split("\\s+"); wordNum += str.length; lineNum ++;} fw.write("There are " + lineNum + " lines."); fw.write("There are " + wordNum + " words."); fw.close(); fr.close(); } catch (IOException e){ System.out.println("Uh oh, got an IOException error!"); e.printStackTrace(); } }

IO - Advanced topics Redirecting standard IO –System contains setIn, setOut and setErr. System.setIn(new FileInputStream(“x.dat”)); System.setOut(new PrintStream(new FileOutputStream( "out.txt"))); –Standard streams could be from/to anything you want! Compression –Read about ZipInputStream and ZipOutputStream For more information: –jdk documentation –On-line tutorials :