Programme système J
import java.io.*;
public class StudentFile
{
String studentFilePath;
String student;
String gender;
String cgpa_str;
double cgpa;
StudentFile(String path)
{
studentFilePath = path;
}
void display_gender_br(String gndr) throws IOException, FileNotFoundException
{
File student_file = new File(studentFilePath);
FileReader student_reader = new FileReader(student_file);
BufferedReader student_lines = new BufferedReader(student_reader);
while ((student = student_lines.readLine()) != null)
{
gender = student.split(",")[4];
if (gender.equals(gndr))
System.out.println(student);
}
student_reader.close();
student_lines.close();
}
void display_cgpa_br(double min, double max) throws IOException, FileNotFoundException
{
File student_file = new File(studentFilePath);
FileReader student_reader = new FileReader(student_file);
BufferedReader student_lines = new BufferedReader(student_reader);
while ((student = student_lines.readLine()) != null)
{
cgpa_str = student.split(",")[2];
cgpa = Double.valueOf(cgpa_str);
if (cgpa>=min && cgpa<=max)
System.out.println(student);
}
student_reader.close();
student_lines.close();
}
// File format : 6235,Kim Blue ,1.85,15-04-1992,F
// id : 4 bytes
// name : 30 bytes
// cgpa : 4 bytes
// date : 10 bytes
// gender : 1 byte
// commas : 4 bytes
// CR LF : 2 bytes
// TOTAL : 55 bytes
void display_gender_racc(String gndr) throws IOException {
File student_file = new File(studentFilePath);
RandomAccessFile std_file = null;
try {
std_file = new RandomAccessFile(student_file, "r");
String gender = null;
String std_rec = null;
String[] fields;
while (std_file.getFilePointer() <= std_file.length() - 2) // checking the end of file
{
std_rec = std_file.readLine();
fields = std_rec.split(",");
gender = fields[4];
if (gender.equals(gndr))
System.out.println(std_rec);
}
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
std_file.close();
}
}
void display_gender_racc2(String gndr) throws IOException {
File student_file = new File(studentFilePath);
RandomAccessFile std_file = null;
try {
std_file = new RandomAccessFile(student_file, "r");
String gender = null;
String std_rec = null;
String[] fields;
byte[] rec = new byte[53];
while(std_file.getFilePointer() <= std_file.length()-2) // checking the end of file
{
std_file.read(rec, 0, 53);
std_rec = new String(rec);
std_file.seek(std_file.getFilePointer()+2);
fields = std_rec.split(",");
gender = fields[4];
if (gender.equals(gndr))
System.out.println(std_rec);
}
} catch (UnsupportedEncodingException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally
{
std_file.close();
}
}
}
Blue-eyed Beetle