Binary I/O 2011.05.06. Outline Text I/O Chap 9. WriteData Chap 9. ReadData Binary I/O Text I/O vs Binary I/O Binary I/O Classes Using DataInputStream/DataOutputStream.

Slides:



Advertisements
Similar presentations
Chapter 19 Binary I/O.
Advertisements

Chapter 10 Ch 1 – Introduction to Computers and Java Streams and File IO 1.
Lecture 31 File I/O -Part 2 COMP1681 / SE15 Introduction to Programming.
 We can use a combination of the File and FileOutputStream to write a series of bytes to a file.
Creating Graphical User Interfaces
1 File Output. 2 So far… So far, all of our output has been to System.out  using print(), println(), or printf() All input has been from System.in 
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Thinking in Objects Outline The this Reference [Sample code] PressEnter.java.
Network Programming CS3250. References Core Java, Vol. II, Chapter 3. Book examples are available from
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L07 (Chapter 18) Binary I/O.
CIS 234: More File Input & Output Dr. Ralph D. Westfall May, 2007.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 18 Binary I/O.
Applet and Multimedia Outline Developing Applets Applet Life-Cycle Method Passing Strings to Applets [Sample code] DisplayLabel.html.
String and Text I/O Outline The String Class Immutable Strings and Interned Strings String Comparisons String Length, Characters,
Java class Outline Defining a method Calling Method Passing parameters [Sample code] TestMethod.java 、 TestMethod2.java 、 GCD.java 、 prime.java.
Lecture 30 Streams and File I/O COMP1681 / SE15 Introduction to Programming.
2. I/O Text Files CCSA225 - Advanced Java Programming Sarah Alodan
CS203 Programming with Data Structures Input and Output California State University, Los Angeles.
INF120 Basics in JAVA Programming AUBG, COS dept, Fall semester 2013 Reference books: Malik D.S., Java Programming, From Problem Analysis to Program Design,
Introduction to Java Y.Daniel Liang
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 19 Binary I/O.
Input and Output in Java Monday, February 10, 2014 Nancy L. Harris.
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 16 Simple Input and Output 与人玫瑰,手有余香.
Chapter 17 Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams  Use JFileChooser F Text Input.
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.
Program data (instance variables, local variables, and parameters) is transient, because its lifetime ends with the program...if not, before. Sometimes.
JAVA I/O © EnhanceEdu, IIIT Hyderabad. Contents 3/29/2010EnhanceEdu, IIIT - H 2  Command Line I/O  File Class  Streams  Byte Streams [Low level and.
Binary Files, Random Access Files Binary Files The way data is stored in memory is sometimes called the raw binary format. Data can be stored in.
1 Week 12 l Overview of Streams and File I/O l Text File I/O Streams and File I/O.
Java Chapter 9 File Input and Output. Objectives In this chapter you will: Learn how bits, characters, records, and files fit into a data hierarchy Explore.
© Amir Kirsh Files and Streams in Java Written by Amir Kirsh.
Streams & Files. Java I/O Classes Saving data to / Loading data from files Two Choices: Binary-Formatted or Text-Formatted Data – int x = 1234; – Binary.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 19 Binary I/O.
1 Chapter 19 Binary I/O. 2 Motivations F Data stored in a text file – is represented in human-readable form –Text file –Readable –Java source programs.
Chapter 15: Input and Output F Stream Classes F Processing External Files F Data Streams F Print Streams F Buffered Streams F Parsing Text Files F Random.
Chapter 15: Input and Output
1.  At the end of this slide, student able to:  Applet & Advanced Graphic  Learn how the Web browser controls and executes the applet  Learn how to.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Files in Java 1.
1 Chapter 16 Simple Input and Output. 2 Objectives F To discover file properties, delete and rename files using the File class (§16.2). F To understand.
File Input and Output Appendix E © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Scanner as an iterator Several classes in the Java standard class library Are iterators Actually, the Scanner class is an iterator  The hasNext returns.
1 Putting Streams to use. 2 Stream Zoo C++ gives you istream, ostream, iostream, ifstream, ofstream, fstream, wistream, wifstream, istrsteam… (18) Java.
The Java IO System Different kinds of IO Different kinds of operations
IO in java.
BINARY I/O IN JAVA CSC 202 November What should be familiar concepts after this set of topics: All files are binary files. The nature of text files.
IO in java.
Chapter 17 Input and Output
CIS265/506 Cleveland State University – Prof. Victor Matos
Text File Input/Output
Reading from a file and Writing to a file
Chapter 17 Binary I/O.
Input and Output in Java
Streams and File I/O.
Text File Input/Output
CHAPTER 5 JAVA FILE INPUT/OUTPUT
Chapter 17 Binary I/O 1.
Chapter 17 Binary I/O Dr. Clincy - Lecture.
JAVA IO.
Input and Output in Java
Text I/O.
Reading and Writing Text Files
Chapter 16 Simple Input and Output
Input and Output in Java
Input and Output in Java
Reading and Writing Text Files
CS 240 – Advanced Programming Concepts
Podcast Ch23c Title: Binary Files
Chapter 17 Binary I/O.
David Davenport Spring 2005
Presentation transcript:

Binary I/O

Outline Text I/O Chap 9. WriteData Chap 9. ReadData Binary I/O Text I/O vs Binary I/O Binary I/O Classes Using DataInputStream/DataOutputStream Copy File [Sample code] TestFileStream.java 、 TestDataStream.java 、 Copy.java

Text I/O Write: // Write text to a file named temp.txt PrintWriter output = new PrintWriter(“temp.txt”); // Invoke the print method from the object to write a string into a file. Output.print(“Java 101”) // Closed the file. Output.close();

Read: // Using Scanner class Scanner input = new Scanner(new File(“temp.txt”)); System.out.println(input.nextLine()); // input.nextLine() 回傳 “Java 101”

Chap 9. WriteData

Chap 9. ReadData

Binary I/O All files are store in binary format. No need to encode and decode.

Binary I/O Program Input object created from an input class Output object created from an output class File 01011… …1011 Input stream output stream The program receives data and send data

Text I/O vs Binary I/O Text I/O program The Unicode of the character e.g.,”199” The encoding of the character is stored in the file Encoding Decoding Binary I/O program A byte is read/written e.g.,”199” The encoding of the character is stored in the file ※ Text I/O requires encoding and decoding, whereas binary I/O does not ※ 199 = 12 x  0xC7 1

Binary I/O Classes 課本 P.676

程式範例 :  TestFileStream.java

Using DataInputStream/DataOutputStream Data streams constructors: public DataInputStream(InputStream instream) public DataOutputStream(OutputStream outstream) Create data streams: DataInputStream input = new DataInputStream(new FileInputStream(“in.dat”)); DataOutputStream output = new DataOutputStream(new FileOutputStream(“out.dat”));

程式範例 :  TestDataStream.java

BufferedInputStream/BufferedOutputStream BufferedInputStream/BufferedOutputStream can be used to speed up input and output by reducing number of reads and writes. All the methods in BufferedInputStream/BufferedOutputStream are inherited from the InputStream/OutputStream classes

可在 TestDataStream.java 的第 8 行及第 22 行加入 : DataOutputStream output = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("temp.dat"))); DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream("temp.dat")));

Copy File 程式範例 :  Copy.java ※ Java Copy source target

程式練習 :  Programming Exercises 19.2