Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.nio.ByteBuffer;
- public class App {
- public static void main(String[] args) {
- File textFile = new File("textFile.txt");
- File binFile = new File("binFile.bin");
- writeStudentToText(new Student("Petar", "04323", 5, 21), textFile);
- Student student = readStudentFromText(textFile);
- System.out.println("INFO FROM TXT FILE:");
- student.printInfo();
- writeStudentToBin(student, binFile);
- System.out.println("INFO FROM BIN FILE:");
- student = readStudentFromBin(binFile);
- student.printInfo();
- }
- public static void writeStudentToText(Student student, File textFile){
- FileWriter fileWriter = null;
- try {
- fileWriter = new FileWriter(textFile);
- fileWriter.write(student.getName() + "|" + student.getFacNumber() + "|" + student.getGrade() + "|" + student.getAge());
- fileWriter.flush();
- } catch (IOException e) {
- System.out.println("There was an IO error with writing Student to Text!");
- e.printStackTrace();
- } finally {
- if(fileWriter != null) try{
- fileWriter.close();
- } catch (IOException e) {
- System.out.println("Can't close fileWriter(IO error)!");
- e.printStackTrace();
- }
- }
- }
- public static Student readStudentFromText(File textFile) {
- try {
- BufferedReader bf = new BufferedReader(new FileReader(textFile));
- String line = bf.readLine();
- String[] info = line.split("\\|", 4);
- Student student = new Student(info[0], info[1], Double.parseDouble(info[2]), Integer.parseInt(info[3]));
- bf.close();
- return student;
- } catch (IOException e) {
- System.out.println("There was an IO error with reading Student from Text");
- e.printStackTrace();
- return null;
- }
- }
- public static void writeStudentToBin(Student student, File binFile){
- byte[] sName = student.getName().getBytes();
- byte[] sFacNumber = student.getFacNumber().getBytes();
- byte[] sGrade = ByteBuffer.allocate(Double.BYTES).putDouble(student.getGrade()).array();
- byte[] sAge = ByteBuffer.allocate(Integer.BYTES).putInt(student.getAge()).array();
- try {
- FileOutputStream os = new FileOutputStream(binFile);
- os.write(sFacNumber.length);
- os.write(sFacNumber);
- os.write(sName.length);
- os.write(sName);
- os.write(sGrade);
- os.write(sAge);
- os.flush();
- os.close();
- } catch (FileNotFoundException e) {
- System.out.println("Bin File not found");
- e.printStackTrace();
- } catch (IOException e) {
- System.out.println("There was an IO error with writing Student to Bin!");
- e.printStackTrace();
- }
- }
- public static Student readStudentFromBin(File binFile){
- try {
- FileInputStream is = new FileInputStream(binFile);
- byte[] facNum = new byte[is.read()];
- is.read(facNum);
- byte[] name = new byte[is.read()];
- is.read(name);
- byte[] grade = new byte[Double.BYTES];
- is.read(grade);
- byte[] age = new byte[Integer.BYTES];
- is.read(age);
- is.close();
- return new Student(new String(name), new String(facNum), ByteBuffer.wrap(grade).getDouble(), ByteBuffer.wrap(age).getInt());
- } catch (FileNotFoundException e) {
- System.out.println("Bin File not found");
- e.printStackTrace();
- } catch (IOException e) {
- System.out.println("There was an IO error with reading Student to Bin!");
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement