Bài giảng Phát triển phần mềm phân tán - Bài 11: Input and output operations - Vũ Thị Hương Giang
LESSON XI.
Input and Output Operations
Vu Thi Huong Giang
SoICT (School of Information and
Communication Technology)
HUST (Hanoi University of Science
and Technology)
Objectives
• Become familiar with the concept of an I/O
stream
• Understand the difference between binary files
and text files
• Learn how to read data from a file
• Learn how to save data into a file
Content
I. Basic input and output
II. File Manipulation
III.Input and Output with Scanner and
printf
I. Basic input and output
1. Overview
2. Model to do I/O
1. Overview – I/O
• I/O = Input/Output
• In this context it is input to and output from
programs
• Input can be from keyboard or a file
• Output can be to display (screen) or a file
• Advantages of file I/O
– permanent copy
– output from one program can be input to another
– input can be automated (rather than entered manually)
1. Overview-Streams
• All modern I/O is stream-based
• Stream: an object that either delivers data to its
destination (screen, file, etc.) or that takes data from
a source (keyboard, file, etc.)
– it acts as a buffer between the data source and destination
• Input stream: a stream that provides input to a
program
– System.in is an input stream
• Output stream: a stream that accepts output from a
program
– System.out is an output stream
• A stream connects a program to an I/O object
– System.out connects a program to the screen
– System.in connects a program to the keyboard
1. Overview - Package java.io
• This package offers the below classes that handle
Java I/O
java.lang.Object
InputStream
OutputStream
FileInputStream
FileOutputStream
Reader
Writer
InputStreamReader
BufferedReader
OutputStreamWriter
PrintWriter
FileReader
IOException
FileWriter
File
I. Basic input output
1. Over view
2. Model to do I/O
2. Model to do I/O
import java.io.*;
• Open the stream
• Use the stream (read, write, or both)
• Close the stream
9
Opening a stream
• There is data external to your program that you
want to get, or you want to put data somewhere
outside your program
• When you open a stream, you are making a
connection to that external place
• Once the connection is made, you forget about the
external place and just use the stream
10
Example of opening a stream
• A FileReader is used to connect to a file that will be
used for input:
FileReader fileReader =
new FileReader(fileName);
• The fileName specifies where the (external) file is to
be found
• You never use fileName again; instead, you use
fileReader
11
Using a stream
• Some streams can be used only for input, others
only for output, still others for both
• Using a stream means doing input from it or output
to it
• But it’s not usually that simple--you need to
manipulate the data in some way as it comes in or
goes out
12
Example of using a stream
int charAsInt;
charAsInt = fileReader.read( );
• fileReader.read():
– reads one character
– returns it as an integer,
– returns -1 if there are no more characters to read
• You can cast from int to char:
char ch = (char) fileReader.read( );
13
Manipulating the input data
• BufferedReader class:
– convert integers to characters
– read whole lines
BufferedReader bufferedReader =
new BufferedReader(fileReader);
14
Reading lines
String s;
s = bufferedReader.readLine( );
• A BufferedReader will return null if there is
nothing more to read
15
Closing
bufferedReader.close( );
• You should not have more than one opened
stream on the same file.
• You must close a stream before you can open it
again
• Java will normally close your streams for you
when your program ends, but it isn’t good style to
depend on this
16
II. File manipulation
1. Text files versus binary files
2. File Class
3. Text files
4. Byte stream files
5. Object stream files-Serialization
1. Binary Versus Text Files
• All data and programs are ultimately just zeros and ones
– each digit can have one of two values, hence binary
– bit is one binary digit
– byte is a group of eight bits
• Text files: the bits represent printable characters
– one byte per character for ASCII, the most common code
– for example, Java source files are text files
– so is any file created with a "text editor"
• Binary files: the bits represent other types of encoded
information, such as executable instructions or numeric data
– these files are easily read by the computer but not humans
– they are not "printable" files
• actually, you can print them, but they will be
unintelligible
• "printable" means "easily readable by humans when
printed"
Java: Text Versus Binary Files
• Text files are more readable by humans
• Binary files are more efficient
– computers read and write binary files more easily than
text
• Java binary files are portable
– they can be used by Java on different machines
– Reading and writing binary files is normally done by a
program
– text files are used only to communicate with humans
II. File manipulation
1. Text files versus binary files
2. FileClass
3. Text files
4. Byte stream files
5. Object stream files-Serialization
Tải về để xem bản đầy đủ
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Phát triển phần mềm phân tán - Bài 11: Input and output operations - Vũ Thị Hương Giang", để tải tài liệu gốc về máy hãy click vào nút Download ở trên
File đính kèm:
bai_giang_phat_trien_phan_mem_phan_tan_bai_11_input_and_outp.pdf

