Bài giảng lập trình java cơ bản chương 5 nhập xuất GV võ hoàng phương dung

19 220 0
Bài giảng lập trình java cơ bản  chương 5 nhập xuất   GV  võ hoàng phương dung

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

LẬP TRÌNH JAVA Chương NHẬP XUẤT GV: Võ Hoàng Phương Dung Nội dung Giới thiệu  File  Random Access File  I/O stream  2/25 Giới thiệu  Nhập xuất Java phân loại • Theo thứ tự truy xuất Truy xuất ngẫu nhiên – Random Access Files: cho phép đọc ghi vị trí  Truy xuất tuần tự: đọc ghi theo thứ tự  • Theo đặc điểm liệu Nhập xuất nhị phân (Nhập xuất byte)  Nhập xuất ký tự  3/25 Giới thiệu  Các lớp nhập xuất Java • Thuộc gói java.io.* • Được tổ chức theo cấu trúc kế thừa Cấp thấp: đọc ghi trực tiếp thiết bị  Cấp cao: đọc ghi thông qua đệm  4/25 File  java.io.File • Dùng để biểu diễn tên file thư mục  Hàm khởi tạo • File(String pathname); • File(String parent, String child); • File(File parent, String child);  Ví dụ: File file = new File("c:/aFolder/aFile.txt"); 5/25 File boolean exists(): kiểm tra có tồn tên file hay thư mục  String getAbsolutePath(): lấy đường dẫn tuyệt đối  String getName(): trả tên file hay thư mục  String getParent(): trả tên thư mục cha  boolean isDirectory(): kiểm tra thư mục  boolean isFile(): kiểm tra file  String[] list(): liệt kê file & thư mục  6/25 File boolean canRead(): kiểm tra file đọc  boolean canWrite():kiểm tra file ghi  boolean canExecute():kiểm tra file thực thi  boolean delete(): xoá file thư mục  long length(): trả lại chiều dài đường dẫn  boolean mkdir(): tạo thư mục  boolean renameTo(File newname): đổi tên file thư mục  7/25 Random Access Files Cho phép ta truy nhập trực tiếp vào tệp, đọc, ghi byte vị trí tệp  Hàm khởi tạo  • RandomAccessFile(String name, String mode) throws IOException • RandomAccessFile(File file, String mode) throws IOException Tham số mode: -“r”: Dùng để đọc -“rw”: Dùng để ghi 8/25 Random Access Files  Các phương thức • long getFilePointer() throws IOException : Trả vị trí trỏ tệp • long length() throws IOException: cho biết số byte hay độ dài tệp • void seek(long offset) throws IOException: Chuyển trỏ tệp offset vị trí kể từ đầu tệp • void close() throws IOException: Khi không cần truy nhập tệp đóng lại 9/25 I/O Stream Stream dãy liệu  Input stream cho việc đọc liệu   Output stream cho việc ghi liệu 10/25 I/O Stream 11/25 I/O Stream  Phân loại • Luồng byte (Byte streams) Đọc ghi liệu theo đơn vị byte  Tất lớp thừa kế từ: InputStream & OutputStream  • Luồng ký tự (Character streams) Đọc ghi liệu theo đơn vị ký tự  Tất lớp thừa kế từ: Reader & Writer  12/25 I/O Stream  Ví dụ luồng Byte import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyBytes { public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(“input.txt"); out = new FileOutputStream(“byteOutput.txt"); int c; while ((c = in.read()) != -1) {out.write(c); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } 13/25 I/O Stream 14/25 I/O Stream  Ví dụ luồng ký tự import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class CopyCharacters { public static void main(String[] args) throws IOException { FileReader in = null; FileWriter out = null; try { in = new FileReader(“input.txt"); out = new FileWriter(“characterOutput.txt"); int c; while ((c = in.read()) != -1){ out.write(c); } } finally { if (in != null) { in.close(); } if (out != null){ out.close(); } } } } 15/25 I/O Stream  Các lớp luồng Byte Tên lớp Ý nghĩa FileInputStream Luồng nhập cho phép đọc liệu từ File FileOutputStream Luồng xuất cho phép ghi liệu xuống File ByteArrayInputStream Luồng nhập liệu từ mảng byte ByteArrayOutputStream Luồng xuất liệu mảng byte ObjectInputStream Luồng nhập Object ObjectOutputStream Luồng xuất Object BufferedInputStream Luồng nhập có đệm BufferedOutputStream Luồng xuất có đệm DataInputStream Luồng nhập liệu kiểu liệu chuẩn DataOutputStream Luồng xuất liệu kiểu liệu chuẩn 16/25 I/O Stream  Các lớp luồng ký tự Tên lớp Ý nghĩa BufferedReader Luồng nhập có đệm BufferedWriter Luồng xuất có đệm CharArrayReader Luồng nhập liệu từ mảng ký tự CharArrayWriter Luồng xuất liệu mảng ký tự InputStreamReader Luồng nhập chuyển luồng byte thành luồng ký tự OutputStreamWriter Luồng xuất chuyển luồng ký tự thành luồng byte StringReader Luồng đọc liệu từ chuỗi StringWriter Luồng xuất liệu chuỗi FileReader Luồng đọc liệu từ File FileWriter Luồng xuất liệu File LineNumberReader Luồng nhập đếm dòng PrintWriter Luồng xuất có định dạng 17/25 I/O Stream  Nhập liệu từ hình Console import java.io.*; class ReadBytes { public static void main(String args[]) throws IOException { byte data[] = new byte[100]; System.out.print("Enter some characters."); System.in.read(data); System.out.print("You entered: "); for(int i=0; i < data.length; i++) System.out.print((char) data[i]); } } 18/25 I/O Stream import java.io.*; class ReadBuffer { public static void main(String args[]) throws IOException { BufferedReader in=new BufferedReader (new InputStreamReader(System.in)); System.out.println("NHAP A:"); int a=Integer.parseInt (in.readLine()); System.out.println("NHAP B:"); int b=Integer.parseInt (in.readLine()); int ketqua; ketqua=a+b; System.out.println("ket qua bai toan a+b la:"+ketqua); } } 19/25 [...]... 15/ 25 I/O Stream  Các lớp cơ bản của luồng Byte Tên lớp Ý nghĩa FileInputStream Luồng nhập cho phép đọc dữ liệu từ File FileOutputStream Luồng xuất cho phép ghi dữ liệu xuống File ByteArrayInputStream Luồng nhập dữ liệu từ 1 mảng byte ByteArrayOutputStream Luồng xuất dữ liệu ra 1 mảng byte ObjectInputStream Luồng nhập các Object ObjectOutputStream Luồng xuất các Object BufferedInputStream Luồng nhập. .. Luồng xuất có đệm DataInputStream Luồng nhập dữ liệu là các kiểu dữ liệu chuẩn DataOutputStream Luồng xuất dữ liệu là các kiểu dữ liệu chuẩn 16/ 25 I/O Stream  Các lớp cơ bản của luồng ký tự Tên lớp Ý nghĩa BufferedReader Luồng nhập có đệm BufferedWriter Luồng xuất có đệm CharArrayReader Luồng nhập dữ liệu từ 1 mảng ký tự CharArrayWriter Luồng xuất dữ liệu ra 1 mảng ký tự InputStreamReader Luồng nhập. .. luồng ký tự OutputStreamWriter Luồng xuất chuyển luồng ký tự thành luồng byte StringReader Luồng đọc dữ liệu từ chuỗi StringWriter Luồng xuất dữ liệu ra chuỗi FileReader Luồng đọc dữ liệu từ File FileWriter Luồng xuất dữ liệu ra File LineNumberReader Luồng nhập đếm dòng PrintWriter Luồng xuất có định dạng 17/ 25 I/O Stream  Nhập dữ liệu từ màn hình Console import java. io.*; class ReadBytes { public... FileOutputStream(“byteOutput.txt"); int c; while ((c = in.read()) != -1) {out.write(c); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } 13/ 25 I/O Stream 14/ 25 I/O Stream  Ví dụ về luồng ký tự import java. io.FileReader; import java. io.FileWriter; import java. io.IOException; public class CopyCharacters { public static void main(String[] args) throws IOException { FileReader in = null; FileWriter...I/O Stream 11/ 25 I/O Stream  Phân loại • Luồng byte (Byte streams) Đọc ghi dữ liệu theo đơn vị byte  Tất cả các lớp thừa kế từ: InputStream & OutputStream  • Luồng ký tự (Character streams) Đọc ghi dữ liệu theo đơn vị ký tự  Tất cả các lớp thừa kế từ: Reader & Writer  12/ 25 I/O Stream  Ví dụ về luồng Byte import java. io.FileInputStream; import java. io.FileOutputStream; import java. io.IOException;... new byte[100]; System.out.print("Enter some characters."); System.in.read(data); System.out.print("You entered: "); for(int i=0; i < data.length; i++) System.out.print((char) data[i]); } } 18/ 25 I/O Stream import java. io.*; class ReadBuffer { public static void main(String args[]) throws IOException { BufferedReader in=new BufferedReader (new InputStreamReader(System.in)); System.out.println("NHAP A:");... A:"); int a=Integer.parseInt (in.readLine()); System.out.println("NHAP B:"); int b=Integer.parseInt (in.readLine()); int ketqua; ketqua=a+b; System.out.println("ket qua bai toan a+b la:"+ketqua); } } 19/ 25 ... vị trí  Truy xuất tuần tự: đọc ghi theo thứ tự  • Theo đặc điểm liệu Nhập xuất nhị phân (Nhập xuất byte)  Nhập xuất ký tự  3/ 25 Giới thiệu  Các lớp nhập xuất Java • Thuộc gói java. io.* • Được...Nội dung Giới thiệu  File  Random Access File  I/O stream  2/ 25 Giới thiệu  Nhập xuất Java phân loại • Theo thứ tự truy xuất Truy xuất ngẫu nhiên – Random Access... StringWriter Luồng xuất liệu chuỗi FileReader Luồng đọc liệu từ File FileWriter Luồng xuất liệu File LineNumberReader Luồng nhập đếm dòng PrintWriter Luồng xuất có định dạng 17/ 25 I/O Stream  Nhập liệu

Ngày đăng: 03/12/2015, 18:23

Tài liệu cùng người dùng

Tài liệu liên quan