Cách làm việc với text file trong ngôn ngữ lập trình Java
import java.io.*;
class fileProcess {
int id;
String name;
void input() throws IOException {
Scanner objSc = new Scanner(System.in);
File f1 = new File("D:\\test.txt");
FileWriter fw = new FileWriter(f1);
try {
for (int i = 0; i < 3; i++) {
System.out.print("id");
this.id = objSc.nextInt();
objSc.nextLine();
System.out.print("name");
this.name = objSc.nextLine();
fw.write(id + " " + this.name + "\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
fw.close();
}
void output() throws IOException {
File f2 = new File("D:\\test.txt");
FileReader fr = new FileReader(f2);
BufferedReader br = new BufferedReader(fr);
String str;
try {
while((str=br.readLine()) !=null) {
System.out.println(str);
}
}catch(FileNotFoundException e) {
e.printStackTrace();
}
br.close();
}
}
public class demo {
public static void main(String[] args) throws IOException {
fileProcess f = new fileProcess();
f.input();
System.out.println("Danh sach sinh vien tu file");
f.output();
}
}
Trương Đình Huy
» Tin mới nhất:
» Các tin khác: