Cách viết chương trình java để xử lý file text
import java.io.*;
import java.util.*;
class student {
int id;
String name;
float GPA;
void input() throws IOException {
Scanner objSc = new Scanner(System.in);
File f = new File("D:\\test.txt");
FileWriter fw = new FileWriter(f);
try {
for (int i = 0; i < 3; i++) {
System.out.println("Thong tin SV " + (i + 1) + ":");
System.out.print("Nhap id:");
this.id = objSc.nextInt();
objSc.nextLine();
System.out.print("Nhap ten:");
this.name = objSc.nextLine();
System.out.print("Nhap GPA:");
this.GPA = objSc.nextFloat();
fw.write(id + "." + this.name + " " + this.GPA + "\n");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
fw.close();
}
void output() throws IOException {
// File f = new File("D:\\test.txt");
FileReader fr = new FileReader("D:\\test.txt");
BufferedReader br = new BufferedReader(fr);
String str = null;
try {
while ((str = br.readLine()) != null)
System.out.println(str);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
fr.close();
br.close();
}
}
public class demo {
public static void main(String[] args) throws IOException {
student f = new student();
f.input();
f.output();
}
}
Trương Đình Huy
» Tin mới nhất:
» Các tin khác: