InputStream Vào từ file: try { •InputStream input = new FileInputStream"input.txt"; while input .read != -1 { //do something with data..... OutputStream Ra file: try { •OutputStream out
Trang 1Lập hướng đối tượng
Vào ra file với Java
Giảng viên: TS Nguyễn Mạnh Hùng
Học viện Công nghệ Bưu chính Viễn thông (PTIT)
Trang 2Nội dung
Trang 3InputStream/OutputStream
Trang 4InputStream
Vào từ file:
try {
•InputStream input = new FileInputStream("input.txt");
while (( input read()) != -1) {
//do something with data
}
input close();
} catch (IOException e){
System.out.println(e);
}
Trang 5OutputStream
Ra file:
try {
•OutputStream output = new
FileOutputStream("output.txt");
output write(1111111);
output close();
} catch (IOException e){
System.out.println(e);
}
Trang 6BufferedInputStream/ BufferedOutputStream
Trang 7BufferedInputStream
Vào từ file:
try {
BufferedInputStream input = new
BufferedInputStream( new FileInputStream("input.txt"));
byte [] in = new byte [1024];
while (( input read(in)) != -1) {
//do something with data
}
input close();
} catch (IOException e){
System.out.println(e);
}
Trang 8BufferedOutputStream
Ra file:
try {
BufferedOutputStream output = new
BufferedOutputStream( new FileOutputStream("output.txt"));
output write(int input);
output write(byte[] buff, int start, int length);
output close();
} catch (IOException e){
System.out.println(e);
}
Trang 9DataInputStream/ DataOutputStream
Trang 10DataInputStream
Vào từ file:
try {
DataInputStream input = new
DataInputStream( new FileInputStream("input.txt"));
String in = input readUTF();
//do something with data
input close();
} catch (IOException e){
System.out.println(e);
}
Trang 11DataOutputStream
Ra file:
try {
DataOutputStream output = new
DataOutputStream( new FileOutputStream("output.txt"));
output writeUTF("some thing to write");
output close();
} catch (IOException e){
System.out.println(e);
}
Trang 12BufferedReader/ BufferedWriter
Trang 13BufferedReader
Vào từ file:
BufferedReader input = new
BufferedReader( new FileInputStream("input.txt"));
Trang 14BufferedWriter
Ra file:
BufferedWriter output = new
BufferedWriter( new FileOutputStream("output.txt"));
Trang 15InputStreamReader/ OutputStreamWriter
Trang 16InputStreamReader
Vào từ file:
InputStreamReader input = new
InputStreamReader( new FileInputStream("input.txt"));
Trang 17OutputStreamWriter
Ra file:
OutputStreamWriter output = new
OutputStreamWriter( new FileOutputStream("output.txt"));
Trang 18Bài tập
Viết chương trình nhập một ô số sudoku từ
bàn phím rồi ghi ra file
Viết chương trình đọc nội dung một ô số
sudoku từ file rồi hiển thị lên màn hình
Trang 19Questions?