EEC 484/584 Computer Networks

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

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.
III. Streams. Introduction Often a program needs to bring in information from an external source or to send out information to an external destination.
Files from Ch4. File Input and Output  Reentering data all the time could get tedious for the user.  The data can be saved to a file. Files can be input.
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.
Tim Wentz Nathaniel Smith Andrew Johnson. Files in Java Create a new file handle using: ▫new File(String pathname); Pathnames can be either relative or.
Unit 201 FILE IO Types of files Opening a text file for reading Reading from a text file Opening a text file for writing/appending Writing/appending to.
Sakir YUCEL MISM/MSIT Carnegie Mellon University Lecture: I/O
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.
Class Decimal Format ► Import package java.text ► Create DecimalFormat object and initialize ► Use method format ► Example: import java.text.DecimalFormat.
1 Text File I/O Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
1 Streams Overview l I/O streams l Opening a text file for reading l Reading a text file l Closing a stream l Reading numbers from a text file l Writing.
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 ->
7/2/2015CS2621 OO Design and Programming II I/O: Reading and Writing.
פיתוח מונחה עצמים – שפת JAVA קבצים. References קורס "שיטות בהנדסת תוכנה", הפקולטה למדעי המחשב, הטכניון. קורס "מערכות מידע מבוזרות", הפקולטה להנדסת תעשייה.
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/
Two Ways to Store Data in a File Text format Binary format.
Session 05 Java Strings and Files. Exercise Complete the “quick-and-dirty” class CharacterCounter containing only a main() method that displays the number.
5-Oct-15 Air Force Institute of Technology Electrical and Computer Engineering Object-Oriented Programming Design Topic : Streams and Files Maj Joel Young.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
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,
OOP with Java, David J. Barnes Input-Output1 A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated.
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.
I/O in Java Dennis Burford
File IO Basics By Dan Fleck Coming up: Data Streams.
By Rachel Thompson and Michael Deck.  Java.io- a package for input and output  File I/O  Reads data into and out of the console  Writes and reads.
CS101 Lab “File input/Output”. File input, output File : binary file, text file READ/WRITE class of “text file” - File Reading class : FileReader, BufferedReader.
Lecture 5 I/O and Parsing
CIS Intro to JAVA Lecture Notes Set 6 2-June-05.
Read and Write Files  By the end of this lab you will be able to:  Write a file in internal storage  Read a file from internal storage  Write a file.
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.
I/O Basics Java does provide strong, flexible support for I/O related to files and networks. Java’s console based interaction is limited since in real.
CSCI 1100/1202 January 23, Class Methods Some methods can be invoked through the class name, instead of through an object of the class These methods.
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.
CSI 3125, Preliminaries, page 1 Files. CSI 3125, Preliminaries, page 2 Reading and Writing Files Java provides a number of classes and methods that allow.
Java I/O 1. Java I/O (Input and Output) is used to process the input and produce the output based on the input. The java.io package contains all the classes.
CS 116 Object Oriented Programming II Lecture 11 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
1 Input-Output A complex issue in programming language design. The interface to the outside world. –Differences must be accommodated as transparently as.
CS202 Java Object Oriented Programming Input and Output Chengyu Sun California State University, Los Angeles.
IO in java.
IO in java.
OO Design and Programming II I/O: Reading and Writing
Lesson 8: More File I/O February 5, 2008
Program Input/Output (I/O)
Strings and File I/O.
Interactive Standard Input/output
12: The Java I/O System stream model.
CHAPTER 5 JAVA FILE INPUT/OUTPUT
I/O Basics.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
File class File myFile=new File(“c:/javaDemo/aa
JAVA IO.
תרגול מס' 5: IO (קלט-פלט) זרמי קלט וזרמי פלט ((Input & Output Streams,
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Reading and Writing Text Files
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
Web Design & Development Lecture 8
ECE 122 April 14, 2005.
Java IO Packages Prepared by Mrs.S.Amudha AP/SWE
Comp 212: Intermediate Programming Lecture 30 – Stream and File I/O
Presentation transcript:

EEC 484/584 Computer Networks Java Tutorial #2: Java IO Wenbing Zhao Cleveland State University wenbing@ieee.org Materials taken from: http://www.cosc.brocku.ca/mentor/javaio_tutorial

EEC484/584 Computer Networks Java IO To do Java IO, need to Import java.io.* System InputStreamReader BufferedReader File BufferedWriter FileReader FileWriter 6/4/2019 EEC484/584 Computer Networks

EEC484/584 Computer Networks System System.in: standard input (of type: InputStream) System.out: standard output import java.io.*; public class TrivialApplication { public static void main ( String args[] ) throws IOException{ //reads one byte as an integer from standard in int ch = System.in.read(); System.out.println((char)ch); // print to standard out }; } 6/4/2019 EEC484/584 Computer Networks

EEC484/584 Computer Networks InputStreamReader InputStreamReader: a bridge from byte streams to character streams, i.e., it reads bytes and decodes them into characters Constructor: InputStreamReader(InputStream in) Methods: close(); read(); etc. import java.io.*; public class TrivialApplication { public static void main ( String args[] ) throws IOException{ InputStreamReader input = new InputStreamReader(System.in); int ch = input.read(); System.out.println((char)ch); // print to standard out }; } 6/4/2019 EEC484/584 Computer Networks

EEC484/584 Computer Networks BufferedReader BufferedReader: is used to improve IO efficiency, i.e., it may read more bytes from the underlying stream than necessary to satisfy current read operation Constructor: BufferedReader(Reader in) Methods: close(); read(); readLine(); etc. BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 6/4/2019 EEC484/584 Computer Networks

EEC484/584 Computer Networks File File: used to create a file with designated filename File inputFile = new File("testFile.txt"); // relative path, in current working directory File outputFile = new File("/home/users/2p92/outFile.txt"); // absolute path, separators OS dependent 6/4/2019 EEC484/584 Computer Networks

EEC484/584 Computer Networks BufferedWriter Make write operation more efficient: allows for one writing step by providing data output storage which can then be outputted efficiently Constructor: BufferedWriter(Writer out); Methods: close(); flush(); newLine(); write(int c), etc. 6/4/2019 EEC484/584 Computer Networks

EEC484/584 Computer Networks FileReader Allows an application to gain access to character input from file Constructor: FileReader(File file) Methods: close(); read(); etc. File inputFile = new File("test.dat"); FileReader Joe = new FileReader(inputFile); 6/4/2019 EEC484/584 Computer Networks

EEC484/584 Computer Networks FileWriter Allows an application to place character output in the designated file Constructor: FileWriter(File file); Methods: close(); flush(); write(); etc. File inputFile = new File("test.dat"); FileReader Joe = new FileReader(inputFile); File outputFile = new File("out.dat"); FileWriter out = new FileWriter(outputFile); int c; While ((c = joe.read() !=-1){ out.write(c); } joe.close(); out.close(); 6/4/2019 EEC484/584 Computer Networks

EEC484/584 Computer Networks A Useful Example public class IOTest {   public static void main(String[] args) throws IOException {       BufferedReader br = new BufferedReader(new FileReader(new File("iotest.dat")));       BufferedWriter bw = new BufferedWriter(new FileWriter(new File("iotestout.dat")));       while(true) { String s = br.readLine();      if(null == s) break;           bw.write("Out: "+s); bw.newLine();       }       bw.flush();       br.close();       bw.close();   } } 6/4/2019 EEC484/584 Computer Networks