Bài giảng Các luồng vào ra

Nội dung

„Khái niệm vềluồng dữliệu

„Luồng và tệp

„Lớp File

„Truy cập tệp tuần tự

„Truy cập tệp ngẫu nhiên

pdf45 trang | Chuyên mục: Java | Chia sẻ: dkS00TYs | Lượt xem: 2124 | Lượt tải: 2download
Tóm tắt nội dung Bài giảng Các luồng vào ra, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
 thụ dữ
liệu
Các luồng vào ra 15Nguyễn Việt Hà
Sử dụng bộ đệm
„ Bộ đệm là một kỹ thuật để tăng tính hiệu quả của thao 
tác vào / ra
… đọc và ghi dữ liệu theo khối
… giảm số lần thao tác với thiết bị
„ Thay vì ghi trực tiếp tới thiết bị thì chương trình ghi lên 
bộ đệm
… khi bộ đệm đầy thì dữ liệu được ghi ra thiết bị theo khối
… có thể ghi vào thời điểm bất kỳ bằng phương thức flush()
„ Thay vì đọc trực tiếp từ thiết bị thì chương trình đọc từ
bộ đệm
… khi bộ đệm rỗng thì dữ liệu được đọc theo khối từ thiết bị
Các luồng vào ra 16Nguyễn Việt Hà
Nhập xuất qua thiết bị chuẩn
Console I/O
„ System.out cho phép in ra luồng ra chuẩn
… là đối tượng của lớp PrintStream
„ System.err cho phép in ra luồng thông báo
lỗi chuẩn
… là đối tượng của lớp PrintStream
„ System.in cho phép đọc vào từ thiết bị vào
chuẩn
… là đối tượng của lớp InputStream
Các luồng vào ra 17Nguyễn Việt Hà
Đọc dữ liệu từ luồng vào chuẩn
„ System.in không sử dụng được trực tiếp
„ Chúng ta muốn đọc một dòng ký tự
1. tạo đối tượng luồng ký tự
(InputStreamReader)
2. tạo đối tượng luồng có bộ đệm 
(BufferedReader)
Các luồng vào ra 18Nguyễn Việt Hà
Ví dụ:
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(reader);
---
String s;
try {
s = in.readLine();
}
catch (IOException e) {...}
Các luồng vào ra 19Nguyễn Việt Hà
Lớp File
„ Một trong các nguồn và đích dữ liệu thông 
thường là tệp
„ Lớp File cung cấp các chức năng cơ bản
để thao tác với tệp
…nằm trong gói java.io
… tạo tệp, mở tệp, các thông tin về tệp và thư
mục
Các luồng vào ra 20Nguyễn Việt Hà
Tạo đối tượng File
„ File myFile;
„ myFile = new File(“data.txt”);
„ myFile = new File(“myDocs”, “data.txt”);
„ Thư mục cũng được coi như là một tệp
…File myDir = new File(“myDocs”);
…File myFile = new File(myDir, “data.txt”);
…có phương thức riêng để thao tác với thư mục 
Các luồng vào ra 21Nguyễn Việt Hà
Các phương thức
„ Tên tệp
… String getName()
… String getPath()
… String getAbsolutePath()
… String getParent()
… boolean renameTo(File newName)
„ Kiểm tra tệp
… boolean exists()
… boolean canWrite()
… boolean canRead()
… boolean isFile()
… boolean isDirectory()
… boolean isAbsolute()
Các luồng vào ra 22Nguyễn Việt Hà
Các phương thức (2)
„ Nhận thông tin
…long lastModified()
…long length()
…boolean delete()
„ Thư mục
…boolean mkdir()
…String[] list()
Các luồng vào ra 23Nguyễn Việt Hà
Thao tác với tệp ký tự
„ Đọc từ tệp
…FileReader: đọc ký tự từ tệp
…BufferedReader: đọc có bộ đệm (đọc từng 
dòng readLine())
„ Ghi ra tệp
…FileWriter: ghi ký tự ra tệp
…PrintWriter: ghi theo dòng (print() và println())
Các luồng vào ra 24Nguyễn Việt Hà
Ví dụ: Đọc vào từ tệp
File file = new File(”data.txt”);
FileReader reader = new FileReader(file);
BufferedReader in = new BufferedReader(reader);
String s;
try {
s = in.readLine();
}
catch (IOException e) {
}
Các luồng vào ra 25Nguyễn Việt Hà
Ví dụ: Đọc vào (cont.)
File file = new File(”data.txt”);
FileReader reader = new FileReader(file);
BufferedReader in = new BufferedReader(reader);
Abc abc = new Abc();
abc.read(in);
abc.doSomething();
Các luồng vào ra 26Nguyễn Việt Hà
Ví dụ: Đọc vào (cont.)
class Abc {
public void read(BufferedReader in) {
String s;
try {
s = in.readLine();
...
}
catch (IOException e) {…}
}
public void doSomething() {...}
...
}
Các luồng vào ra 27Nguyễn Việt Hà
Ví dụ: Ghi ra tệp
File file = new File(”data.out”);
FileWriter writer = new FileWriter(file);
PrintWriter out = new PrintWriter(writer);
String s = ”Hello”;
try {
out.println(s);
out.close();
}
catch (IOException e) {
}
Các luồng vào ra 28Nguyễn Việt Hà
Ví dụ: Ghi ra (cont.)
class Abc {
...
public void write(PrintStream out) {
...
try {
out.println(s);
out.close();
}
catch (IOException e) {...}
}
}
Các luồng vào ra 29Nguyễn Việt Hà
Ví dụ: Ghi ra (cont.)
class Abc {
...
public String write() {
String buf;
buf += ...
return buf;
}
}
Các luồng vào ra 30Nguyễn Việt Hà
Ví dụ: File copy
import java.io.*;
public class CopyFile {
public static void main(String args[]) {
try {
FileReader src = new FileReader(args[0]);
BufferedReader in = new BufferedReader(src);
FileWriter des = new FileWriter(args[1]);
PrintWriter out = new PrintWriter(des);
String s;
s = in.readLine();
while (s != null) {
out.println(s);
s = in.readLine();
}
in.close();
out.close();
}
catch (IOException e) {
e.printStackTrace();
} 
}
}
Các luồng vào ra 31Nguyễn Việt Hà
Ví dụ: File copy (2)
import java.io.*;
public class CopyFile2 {
public static void main(String args[]) {
try {
FileReader src = new FileReader(args[0]);
FileWriter des = new FileWriter(args[1]);
char buf[] = new char[128];
int charsRead;
charsRead = src.read(buf);
while (charsRead != -1) {
des.write(buf, 0, charsRead);
charsRead = src.read(buf);
}
src.close();
des.close();
}
catch (IOException e) {
e.printStackTrace();
} 
}
}
Các luồng vào ra 32Nguyễn Việt Hà
Thao tác với tệp dữ liệu (tuần tự)
„ Đọc dữ liệu
… FileInputStream: đọc dữ liệu từ tệp
… DataInputStream: đọc dữ liệu kiểu nguyên thủy
…ObjectInputStream: đọc đối tượng
„ Ghi dữ liệu
… FileOutputStream: ghi dữ liệu ra tệp
… DataOutputStream: ghi dữ liệu kiểu nguyên thủy
…ObjectOutputStream: ghi đối tượng
Các luồng vào ra 33Nguyễn Việt Hà
DataInputStream/DataOutputStream
„ DataInputStream: đọc các dữ liệu nguyên 
thủy
… readBoolean, readByte, readChar, readShort, 
readInt, readLong, readFloat, readDouble
„ DataOutputStream: ghi các dữ liệu nguyên 
thủy
…writeBoolean, writeByte, writeChar, 
writeShort, writeInt, writeLong, writeFloat, 
writeDouble
Các luồng vào ra 34Nguyễn Việt Hà
Ghi dữ liệu nguyên thủy (tuần tự)
import java.io.*;
public class TestDataOutputStream {
public static void main(String args[]) {
int a[] = {2, 3, 5, 7, 11};
try {
FileOutputStream fout = new FileOutputStream(args[0]);
DataOutputStream dout = new DataOutputStream(fout);
for (int i=0; i<a.length; i++)
dout.writeInt(a[i]);
dout.close();
}
catch (IOException e) {
e.printStackTrace();
} 
}
}
Các luồng vào ra 35Nguyễn Việt Hà
Đọc dữ liệu nguyên thủy (tuần tự)
import java.io.*;
public class TestDataInputStream {
public static void main(String args[]) {
try {
FileInputStream fin = new FileInputStream(args[0]);
DataInputStream din = new DataInputStream(fin);
while (true) {
System.out.println(din.readInt());
} 
}
catch (EOFException e) {
}
catch (IOException e) {
e.printStackTrace();
} 
}
}
Các luồng vào ra 36Nguyễn Việt Hà
Đối tượng “tuần tự”
„ Một đối tượng có thể được lưu trong bộ
nhớ tại nhiều vùng nhớ khác nhau
…các thuộc tính không phải là kiểu nguyên thủy
„ Đối tượng muốn ghi / đọc được phải thuộc 
lớp có cài đặt giao diện Serializable
…đây là giao diện nhãn, không có phương thức
Các luồng vào ra 37Nguyễn Việt Hà
import java.io.Serializable;
class Record implements Serializable {
private String name;
private float score;
public Record(String s, float sc) {
name = s;
score = sc;
}
public String toString() {
return "Name: " + name + ", score: " + score;
}
}
Các luồng vào ra 38Nguyễn Việt Hà
import java.io.*;
public class TestObjectOutputStream {
public static void main(String args[]) {
Record r[] = { new Record("john", 5.0F), 
new Record("mary", 5.5F),
new Record("bob", 4.5F) };
try {
FileOutputStream fout = new FileOutputStream(args[0]);
ObjectOutputStream out = new ObjectOutputStream(fout);
for (int i=0; i<r.length; i++) 
out.writeObject(r[i]);
out.close();
}
catch (IOException e) {
e.printStackTrace();
} 
}
}
Các luồng vào ra 39Nguyễn Việt Hà
import java.io.*;
public class TestObjectInputStream {
public static void main(String args[]) {
Record r;
try {
FileInputStream fin = new FileInputStream(args[0]);
ObjectInputStream in = new ObjectInputStream(fin);
while (true) {
r = (Record) in.readObject();
System.out.println(r);
}
}
catch (EOFException e) {
System.out.println("No more records");
}
catch (ClassNotFoundException e) {
System.out.println("Unable to create object");
}
catch (IOException e) {
e.printStackTrace();
} 
}
}
Các luồng vào ra 40Nguyễn Việt Hà
Lớp RandomAccessFile
„ Là một lớp độc lập (kế thừa trực tiếp từ
Object)
„ Đảm nhận việc đọc và ghi dữ liệu ngẫu
nhiên
…cài đặt các giao diện DataInput và DataOutput
„ Kích thước bản ghi phải cố định
Các luồng vào ra 41Nguyễn Việt Hà
import java.io.*;
public class WriteRandomFile {
public static void main(String args[]) {
int a[] = { 2, 3, 5, 7, 11, 13 };
try {
File fout = new File(args[0]);
RandomAccessFile out;
out = new RandomAccessFile(fout, "rw");
for (int i=0; i<a.length; i++) 
out.writeInt(a[i]);
out.close();
}
catch (IOException e) {
e.printStackTrace();
} 
}
}
Các luồng vào ra 42Nguyễn Việt Hà
import java.io.*;
public class ReadRandomFile {
public static void main(String args[]) {
try {
File fin = new File(args[0]);
RandomAccessFile in = new RandomAccessFile(fin, "r");
int recordNum = (int) (in.length() / 4);
for (int i=recordNum-1; i>=0; i--) {
in.seek(i*4);
System.out.println(in.readInt());
}
}
catch (IOException e) {
e.printStackTrace();
} 
}
}
Các luồng vào ra 43Nguyễn Việt Hà
Lớp Scanner
„ Là lớp mới hỗ trợ nhập dữ liệu, kế thừa 
trực tiếp từ Object (từ Java 1.5)
„ Khởi tạo với đối số là đối tượng vào 
(luồng, tệp, xâu ký tự)
„ Có các phương thức hỗ trợ nhập trực tiếp
…nextType, hasNextType
Các luồng vào ra 44Nguyễn Việt Hà
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
---
Scanner sc;
sc = new Scanner(new File("myNumbers"));
while (sc.hasNextLong()) {
long aLong = sc.nextLong();
}
--
String str = ”2 3 5 7”;
Scanner sc = new Scanner(str);
while (sc.hasNextInt()) {
System.out.println(sc.nextInt());
}
Các luồng vào ra 45Nguyễn Việt Hà
class Abc {
public void read(Scanner sc) {
...
}
public void doSomething() {...}
...
}

File đính kèm:

  • pdfBài giảng Các luồng vào ra.pdf
Tài liệu liên quan