Introduction to directories and files
Introduction to file input and output
How to work with text files
How to work with binary files
How to work with random-access files
Bài tập:
1. Đọc tệp tin text.txt và in nội dung ra màn hình.
Mã nguồn:
import java.io.FileReader; public class MainClass { public static void main(String[] argv) throws Exception { FileReader fr = new FileReader("C:\\text.txt"); int count; char chrs[] = new char[80]; do { count = fr.read(chrs); for (int i = 0; i < count; i++) { System.out.print(chrs[i]); } } while (count != -1); } } |
Mã nguồn:
import java.io.FileReader; import java.io.BufferedReader; public class Main { public static void main(String[] argv) throws Exception { FileReader fr = new FileReader("C:\\text.txt"); BufferedReader br = new BufferedReader(fr); String eachLine = br.readLine(); while (eachLine != null) { if (eachLine != null) { System.out.println(eachLine); } eachLine = br.readLine(); } br.close(); } } |
Mã nguồn:
import java.io.BufferedReader; import java.io.FileReader; public class DocFile { public static void main(String[] args) throws Exception { FileReader fr = new FileReader("C:\\text.txt");//Tạo FileREader BufferedReader br = new BufferedReader(fr); //Tạo BufferReader String line = ""; line = br.readLine();//dòng đầu tiên String val[] = line.split(" "); int n = Integer.parseInt(val[0]); int m = Integer.parseInt(val[1]); int a[][] = new int[n][m]; for (int i = 0; i < n; i++) { line = br.readLine(); if (line != null) { val = line.split(" "); for(int j = 0; j < m; j++) { a[i][j] = Integer.parseInt(val[j]); } } } br.close(); InMang(a, n, m); } //In mảng hai chiều ra màn hình public static void InMang(int A[][], int n, int m) { System.out.println(n+ " " + m); for(int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { System.out.format("% 3d", A[i][j]); } System.out.println(); } } } |
4. Tạo mảng 2 chiều với giá trị nguyên ngẫu nhiên (dùng Math.random()), với số dòng n và số cột m được nhập từ bàn phím. Sau đó ghi giá trị của mảng này vào tệp tin Output.txt theo quý tắc sau: dòng đầu ghi 2 số n và m lần lượt số dòng và số cột của ma trận, n dòng tiếp theo lưu n dòng của ma trận, các số cách nhau 1 khoảng trống. Ví dụ mảng A[3][4] được lưu trong tệp tin Output.txt như sau:
Mã nguồn:
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n, m;
System.out.print("Nhap n, m: ");
n = scanner.nextInt();
m = scanner.nextInt();
int a[][] = new int[n][m];
//Tạo mảng random
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = (int) (Math.random() * 100);
}
}
//Ghi mảng a[n][m] vào tệp tin
try {
FileWriter fw = new FileWriter("output.txt");
fw.write(n + " " + m + "\n");//Ghi dòng đầu gồm 2 số n và m
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
fw.write(Integer.toString(a[i][j]) + " ");
}
fw.write("\n");
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Mã nguồn:
0 nhận xét: