Kĩ thuật lập trình - Chương 3: Luồng nhập xuất trong java

pdf 24 trang vanle 2170
Bạn đang xem 20 trang mẫu của tài liệu "Kĩ thuật lập trình - Chương 3: Luồng nhập xuất trong java", để tải tài liệu gốc về máy bạn click vào nút DOWNLOAD ở trên

Tài liệu đính kèm:

  • pdfki_thuat_lap_trinh_chuong_3_luong_nhap_xuat_trong_java.pdf

Nội dung text: Kĩ thuật lập trình - Chương 3: Luồng nhập xuất trong java

  1. Chương 3: LUỒNG NHẬP XUẤT TRONG JAVA Khoa CNTT ĐH GTVT TP.HCM Khoa CNTT (ĐH GTVT TP.HCM) Java Programming 1 / 24
  2. Nội dung 1 Biệt lệ (Exception) 2 Khái niệm luồng (Stream) 3 Nhập/Xuất chuẩn 4 Truy xuất File Khoa CNTT (ĐH GTVT TP.HCM) Java Programming 2 / 24
  3. 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án chia cho 0. * Parse chuỗi "abc" thành số nguyên. * Khoa CNTT (ĐH GTVT TP.HCM) Java Programming 3 / 24
  4. Phân loại Exception Checked Exception * Được sử dụng trong signature của method * Được kiểm tra tại thời điểm complile-time * Có ý nghĩa là: đây là phương thức có thể gây ra "lỗi" vì vậy khi sử dụng nó cần phải "bắt - catch" ngoại lệ. Khoa CNTT (ĐH GTVT TP.HCM) Java Programming 4 / 24
  5. Phân loại Exception Checked Exception - Ví dụ: method có khai báo exception static public int chia(int s1, int s2) throws Exception { return s1 / s2; } Checked Exception - Ví dụ: gọi method ở trên 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
  6. Phân loại Exception Unchecked Exception * Được sử dụng trong khối lệnh try catch * Được kiểm tra tại thời điểm 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
  7. 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
  8. Stream trong Java Input stream Khoa CNTT (ĐH GTVT TP.HCM) Java Programming 8 / 24
  9. Stream trong Java Output stream Khoa CNTT (ĐH GTVT TP.HCM) Java Programming 9 / 24
  10. Stream trong Java Hierarchy of classes to deal with Input and Output streams: Khoa CNTT (ĐH GTVT TP.HCM) Java Programming 10 / 24
  11. Nhập/Xuất chuẩn trong Java (Standard Streams) Các packages xuất nhập chuẩn * System.in = bàn phím * System.out = màn hình * System.err → thông báo lỗi Khoa CNTT (ĐH GTVT TP.HCM) Java Programming 11 / 24
  12. Nhập/Xuấ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("Nhapa,b,c:"); a = s.nextInt(); b = s.nextInt(); c = s.nextInt(); Khoa CNTT (ĐH GTVT TP.HCM) Java Programming 12 / 24
  13. Nhập/Xuất chuẩn trong Java Xuất dữ liệu ra màn hình System.out.println("a=" + a); Hoặ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
  14. Truy xuất File Các trường hợp truy xuất file: * Truy xuất file nhị phân: dữ liệu trong file được xem như dãy các bytes nhị phân. * Truy xuất file văn bản: dữ liệu trong file được xem như dãy các mã Unicode của các kí tự. * Serialize & Deserialize: truy xuất file lưu trữ các objects. Khoa CNTT (ĐH GTVT TP.HCM) Java Programming 14 / 24
  15. 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 tao chi la noi dato"; out.write(s.getBytes());//ghi day bytes. } catch (Exception ex) {/*xu ly exceptiono day*/} finally{ try {out.close(); } catch (IOException ex) {/*xu ly exceptiono day*/} } } Khoa CNTT (ĐH GTVT TP.HCM) Java Programming 15 / 24
  16. 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
  17. Truy xuất File Ghi file văn bản public static void ghiFile() { File f = new File("vanban.txt"); FileWriter fw = null; try {fw = new FileWriter(f); fw.write("Khi tao chi la noi dato"); 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
  18. Truy xuất File Đọc file văn 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
  19. Truy xuất File Đọc file văn bản: đọc từng dòng 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
  20. Truy xuất File Đọc file văn 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
  21. 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
  22. 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 lst; lst = (ArrayList )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
  23. Bài tập: Bài tập Sử dụng các kiến thức của chương này (exception & streams), xây dựng chương trình minh họa quá trình mua bán hàng hóa. Gồm các chức năng sau: 1 Hiển thị danh sách hàng hóa. 2 Cho phép chọn chức năng mua hàng. 3 Hiển thị thông tin giỏ hàng. 4 Cho phép cập nhật giỏ hàng. 5 Lưu hóa đơn mua hàng. Khoa CNTT (ĐH GTVT TP.HCM) Java Programming 23 / 24
  24. —Hết— Khoa CNTT (ĐH GTVT TP.HCM) Java Programming 24 / 24