Cách xử lý file trong Java
-----------
Ví dụ minh họa
package fileProcess1;
import java.io.*;
class fileProcess {
void writeToFile() throws IOException {
File fo = new File("D:\\data.txt");
FileWriter fw = new FileWriter(fo);
try {
fw.write("Hello World");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
fw.close();
}
void readFromFile() throws IOException {
File fi = new File("D:\\data.txt");
FileReader fr = new FileReader(fi);
BufferedReader br = new BufferedReader(fr);
String str;
try {
while ((str = br.readLine()) != null) {
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
br.close();
fr.close();
}
}
public class demo {
public static void main(String[] args) throws IOException {
fileProcess f = new fileProcess();
f.writeToFile();
f.readFromFile();
}
}
Trương Đình Huy
- 6 công nghệ trong điện thoại bạn dùng mỗi ngày (mà ít khi biết nó hoạt động thế nào)
- Giới thiệu cách cài đặt ArayList trong ngôn ngữ lập trình Java
- DBT là gì? Tìm hiểu Công cụ Transform trong Data Engineering
- Software Testing - Common Testing Techniques (P1)
- Báo Cáo Chuyên Sâu Về Khung Pháp Lý V Của Hội Đồng Đạo Đức Y Sinh Học Đối Với Nghiên Cứu Thu Thập Tín Hiệu Điện Não Bằng Nền Tảng OpenBCI Tại Việt Nam

