0% found this document useful (0 votes)
6 views25 pages

Chapter3 IO

The document provides an overview of exception handling and stream processing in Java. It covers types of exceptions, standard input/output streams, file handling, and serialization/deserialization techniques. Additionally, it includes a practical assignment related to managing a shopping cart system using the discussed concepts.

Uploaded by

shironekoyoko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views25 pages

Chapter3 IO

The document provides an overview of exception handling and stream processing in Java. It covers types of exceptions, standard input/output streams, file handling, and serialization/deserialization techniques. Additionally, it includes a practical assignment related to managing a shopping cart system using the discussed concepts.

Uploaded by

shironekoyoko
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

Ch£°.

E;ng 3: LUá»'NG NHá°-P XUá°HT TRONG JAVA

Khoa CNTT ẢŨH

GTVT TP.HCM

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 1 / 24


Ná»™i dung

1 Biá»ịt lá»ị (Exception)

2 KhÃji niá»ịm luá»"ng (Stream)

3 Nhá0-p/Xuá0¥t chuá°©n

4 Truy xuá°¥t File

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 2 / 24


Exception là g×?

Ảũá»<nh nghẢ©a (Oracle’s java document)


An exception is an event, which occurs during the execution of a program,
that
disrupts the normal flow of the program’s instructions.

VÃ- dụ
* Thá»±c hiá»ịn phép toÃjn chia cho 0.

* Parse chuá»—i "abc" thà nh sá»' nguyÃan.


■k

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming


3 / 24
PhÃín loá°j i Exception

Checked Exception
* ẢŨ.E°á»£c sá»- dụng trong signature cá»§a method

* ẢŨ.E°á»£c kiá»fm tra tá°ji thá»ũi Ả'iá»fm complile-time

* CÃ3 Ã^ nghẢ©a là : Ả'Ãíy là ph£°.E;ng thức cÃ3 thá»f gÃíy ra và "lá»-i"


— vá°-y khi sá»- dụng nÃ3 cá°§n
phá°£i "bá° t - catch" ngoá°ji lá»ị.

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming


4 / 24
PhÃín loá°j i Exception
Checked Exception - VÃ- dụ: method cÃ3 khai bÃjo exception

static public int chia(int s1, int s2) throws Exception


{ return si / s2;
}

Checked Exception - VÃ- dụ: gá»ũi method á»Ỹ trÃan try {


double kq = ExcepDemo.chia(10, 0);//method call
System.out.println("ket qua = " + kq);
} catch (Exception ex) {
System.out.println(ex.getMessage());
}

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 5 / 24


PhÃín loá°j i Exception Unchecked Exception
* ẢŨ.E°á»£c sá»- dụng trong khá»'i lá»ịnh try ... catch ...
* ẢŨ.E°á»£c kiá»fm tra tá°ji thá»ũi Ả'iá»fm run-time

Lá»ịnh try catch try {


//khoi lenh co kha nang xay ra loi } catch (Exception ex) {
//khoi lenh xu ly khi xay ra loi }
finally{
//khoi lenh luon duoc thu hien du xay ra loi hay khong }

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 6 / 24


Stream trong Java

Stream là g×?
* In computer science Streams are defined as the sequence of data
elements that
is made available over time. It represents a Source (which generates the
data in
the form of Stream) and a destination (which consumes or read data available
as Stream).
* In other simple words it is a flow of data from which you can read or
write data
to it.

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming


7 / 24
Stream trong Java

Input stream

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 8 / 24


Stream trong Java

Output stream

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 9 / 24


Stream trong Java

Hierarchy of classes to deal with Input and Output streams:

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 10 / 24


Nhá0-p/Xuá0¥t chuá°©n trong Java (Standard Streams)

CÃjc packages xuá°¥t nhá°-p chuá°©n

* System.in = bà n phÃ-m

* System.out = mà n h×nh

* System.err ất' thÃ'ng bÃjo lá»—i

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 11 / 24


Nhá0-p/Xuá0¥t chuá°©n trong Java

Nhá°-p dá» liá»ịu từ bà n phÃ-m int a, b, c;


Scanner s = new Scanner(System.in);//su dung System.in
System.out.print("Nhap a, b, c: "); a = s.nextInt(); b =
s.nextlnt(); c = s.nextlnt();

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 12 / 24


Nhá0-p/Xuá0¥t chuá°©n trong Java

Xuá°¥t dá» liá»ịu ra mà n h×nh

System.out.println("a = " + a);


H0á°-c:
String kq = String.format("max(%d,%d,%d) = %d", a,b,c,m);
System.out.println(kq) ;

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 13 / 24


Truy xuá°¥t File

CÃjc tr.E°á»ũng hợp truy xuá°¥t file:


* Truy xuá°¥t file nhá»< phÃín: dá» liá»ịu trong file Ả'.E°á»£c xem nhỉ°
dãy cÃjc bytes nhá»< phÃín.

* Truy xuá°¥t file vẢfn bá°£n: dá» liá»ịu trong file Ả'.E°á»£c xem nhf°
dãy cÃjc mã Unicode
cá»§a cÃjc kÃ- tá»±. *

* Serialize & Deserialize: truy xuá°¥t file lfi°u trá» cÃjc objects.

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming


14 / 24
Truy xuá°¥t File Ghi file nhá»< phÃín
public static void ghiFile() {
FileOutputStream out = null; try {
File f = new File("tho.dat"); out = new FileOutputStream(f);
String s = "Khi ta o chi la noi dat o";
out.write(s.getBytes());//ghi day bytes.
} catch (Exception ex) {/*xu ly exception o day*/} finally {
try {out.close();
} catch (IOException ex) {/*xu ly exception o day*/} }
}

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 15 / 24


Truy xuá°¥t File Ảũá»ũc file nhá»< phÃín
public static void docFile() { FileInputStream
in = null; try {File f = new File("tho.dat");
in = new FileInputStream(f); int kq;
do {kq = in.read();
System.out.print((char) kq);
} while (kq != -1);
} catch (Exception ex) {/*...*/} finally {try
{in.close();
} catch (IOException ex) {/*...*/}
}
}

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 16 / 24


Truy xuá°¥t File Ghi file vẢfn bá°£n
public static void ghiFile() {
File f = new File("vanban.txt");
FileWriter fw = null; try {
fw = new FileWriter(f);
fw.write("Khi ta o chi la noi dat o");
fw.write(System.getProperty("line.separator")) ;
fw.write("Khi ta di dat bong hoa tam hon");
} catch (Exception ex) {/*...*/} finally {try {fw.close();
} catch (IOException ex) {/*...*/}
}
}

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 17 / 24


Truy xuá°¥t File
Ảũá»ũc file vẢfn bá°£n: Ả'á»ũc từng kÃ- tá»±

public static void docFile() {


File f = new File("vanban.txt");
FileReader fr = null;
try {if (!f.exists()) {throw new Exception("File khong ton tai");} fr = new
FileReader(f);
int kq;
while ((kq = fr.read()) != -1) {System.out.print((char) kq);}
}catch (Exception ex) {/*...*/}finally { try {fr.close();
} catch (IOException ex) {/*...*/}

}
}
Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming
18 / 24
Truy xuá°¥t File
Ảũá»ũc file vẢfn bá°£n: Ả'á»ũc từng dÃ2ng public static
void docFileByScanner() { try {File f = new
File("vanban.txt"); if (!f.exists()) {
throw new Exception("File khong ton tai");
}
Scanner scan = new Scanner(f);
String line;
while (scan.hasNextLine()) { line = scan.nextLine();
System.out.println(line);
}scan.close();
} catch (Exception ex) {/*...*/}
}

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 19 / 24


Truy xuá°¥t File

Ảũá»ũc file vẢfn bá°£n: sá»- dụng buffer


public static void docFileByBufferChar() { try {
File f = new File("vanban.txt");
FileReader read = new FileReader(f); BufferedReader b =
new BufferedReader(read); String d;
while ((d = b.readLine()) != null)
{ System.out.println(d);
}
b .close () ;
} catch (Exception ex) {/*...*/}
}

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 20 /


24
Serialize & Deserialize

Serialize
public static void ghiDSHHSerialize() {
File f = new File("hh.dat");
FileOutputStream fo = null;
ObjectOutputStream out = null; try {
fo = new FileOutputStream(f); out = new
ObjectOutputStream(fo);
out.writeObject(HangHoa.dsHangHoa());//note! } catch
(Exception ex) {/*...*/} finally { try {
out .close(); fo. close () ;
} catch (IOException ex) {/*...*/}
}
}

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 21 /


24
Serialize & Deserialize

Deserialize
public static void docDSHHSerialize() {
File f = new File("hh.dat");
FileInputStream fo = null;
ObjectInputStream out = null; try {
fo = new FileInputStream(f); out = new
ObjectInputStream(fo); ArrayList<HangHoa> lst;
lst = (ArrayList<HangHoa>)out.readObject(); for(HangHoa
h:lst)
System.out.println(h.toString() );
} catch (Exception ex) {/*...*/} finally { try {
out.close();fo.close();
} catch (IOException ex) {/*...*/}}
}

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 22 /


24
BÃ i tá °-p:

BÃ i tá °-p
Sá»- dụng cÃjc kiá°£,n thức cá»§a ch.E°.Ejng nà y (exception
xÃíy dá»±ng ch.E°.Ejng & streams),
tr×nh minh há»ũa quÃj tr×nh mua bÃjn hà ng hÃ3a. Gá»"m cÃjc
nẢfng sau: chức
1 Hiá»fn thá»< danh sÃjch hà ng hÃ3a.

2 Cho phép chá»ũn chức nẢfng mua hà ng.

3 Hiá»fn thá»< thÃ'ng tin giá»ũ hà ng.

4 Cho phép cá°-p nhá°-t giá»ũ hà ng.

5 LỄ°u hÃ3a Ả'.Ejn mua hà ng.

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming


23 / 24
â€"Há0itâ€"

Khoa CNTT (ẢŨH GTVT TP.HCM) Java Programming 24 / 24

You might also like