Advertisement
Zeshin

txtToBin

Apr 13th, 2022
1,068
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.92 KB | None | 0 0
  1. import java.io.*;
  2. import java.nio.ByteBuffer;
  3. public class App {
  4.  
  5.     public static void main(String[] args) {
  6.         File textFile = new File("textFile.txt");
  7.         File binFile = new File("binFile.bin");
  8.         writeStudentToText(new Student("Petar", "04323", 5, 21), textFile);
  9.         Student student = readStudentFromText(textFile);
  10.         System.out.println("INFO FROM TXT FILE:");
  11.         student.printInfo();
  12.         writeStudentToBin(student, binFile);
  13.         System.out.println("INFO FROM BIN FILE:");
  14.         student = readStudentFromBin(binFile);
  15.         student.printInfo();
  16.     }
  17.  
  18.  
  19.     public static void writeStudentToText(Student student, File textFile){
  20.         FileWriter fileWriter = null;
  21.         try {
  22.             fileWriter = new FileWriter(textFile);
  23.             fileWriter.write(student.getName() + "|" + student.getFacNumber() + "|" + student.getGrade() + "|" + student.getAge());
  24.             fileWriter.flush();
  25.         } catch (IOException e) {
  26.             System.out.println("There was an IO error with writing Student to Text!");
  27.             e.printStackTrace();
  28.         } finally {
  29.             if(fileWriter != null) try{
  30.                 fileWriter.close();
  31.             } catch (IOException e) {
  32.                 System.out.println("Can't close fileWriter(IO error)!");
  33.                 e.printStackTrace();
  34.             }
  35.         }
  36.  
  37.     }
  38.  
  39.     public static Student readStudentFromText(File textFile) {
  40.         try {
  41.             BufferedReader bf = new BufferedReader(new FileReader(textFile));
  42.             String line = bf.readLine();
  43.             String[] info = line.split("\\|", 4);
  44.             Student student = new Student(info[0], info[1], Double.parseDouble(info[2]), Integer.parseInt(info[3]));
  45.             bf.close();
  46.             return student;
  47.         } catch (IOException e) {
  48.             System.out.println("There was an IO error with reading Student from Text");
  49.             e.printStackTrace();
  50.             return null;
  51.         }
  52.     }
  53.  
  54.     public static void writeStudentToBin(Student student, File binFile){
  55.         byte[] sName = student.getName().getBytes();
  56.         byte[] sFacNumber = student.getFacNumber().getBytes();
  57.         byte[] sGrade = ByteBuffer.allocate(Double.BYTES).putDouble(student.getGrade()).array();
  58.         byte[] sAge = ByteBuffer.allocate(Integer.BYTES).putInt(student.getAge()).array();
  59.         try {
  60.             FileOutputStream os = new FileOutputStream(binFile);
  61.             os.write(sFacNumber.length);
  62.             os.write(sFacNumber);
  63.             os.write(sName.length);
  64.             os.write(sName);
  65.             os.write(sGrade);
  66.             os.write(sAge);
  67.             os.flush();
  68.             os.close();
  69.         } catch (FileNotFoundException e) {
  70.             System.out.println("Bin File not found");
  71.             e.printStackTrace();
  72.         } catch (IOException e) {
  73.             System.out.println("There was an IO error with writing Student to Bin!");
  74.             e.printStackTrace();
  75.         }
  76.     }
  77.  
  78.     public static Student readStudentFromBin(File binFile){
  79.         try {
  80.             FileInputStream is = new FileInputStream(binFile);
  81.             byte[] facNum = new byte[is.read()];
  82.             is.read(facNum);
  83.             byte[] name = new byte[is.read()];
  84.             is.read(name);
  85.             byte[] grade = new byte[Double.BYTES];
  86.             is.read(grade);
  87.             byte[] age = new byte[Integer.BYTES];
  88.             is.read(age);
  89.             is.close();
  90.             return new Student(new String(name), new String(facNum), ByteBuffer.wrap(grade).getDouble(), ByteBuffer.wrap(age).getInt());
  91.         } catch (FileNotFoundException e) {
  92.             System.out.println("Bin File not found");
  93.             e.printStackTrace();
  94.         } catch (IOException e) {
  95.             System.out.println("There was an IO error with reading Student to Bin!");
  96.             e.printStackTrace();
  97.         }
  98.         return null;
  99.     }
  100. }
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement