Advertisement
Guest User

BinaryFile.java

a guest
Apr 12th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. package horsemanager;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.ObjectInputStream;
  7. import java.io.RandomAccessFile;
  8. import java.util.ArrayList;
  9. import java.util.logging.Level;
  10. import java.util.logging.Logger;
  11.  
  12. public class BinaryFile {
  13.  
  14.     public static final int RecordLength = 102;
  15.     private static final short ShowNameLength = 25;
  16.     private static final short BarnNameLength = 20;
  17.     private static final short CoatColorLength = 11;
  18.     private static final short AgeLength = 2;
  19.     private static short pointer = 0;
  20.     private static int ID = 0;
  21.     public final static String FILE_NAME = "/Users/hanaezz/Desktop/horseRecords.dat";
  22.     static private RandomAccessFile raf;
  23.    
  24.     public BinaryFile() throws IOException {
  25.         File f = new File(FILE_NAME);
  26.             if (!f.exists()) {
  27.                     f.createNewFile();
  28.                 }
  29.             raf = new RandomAccessFile(FILE_NAME, "rw");
  30.     }
  31.    
  32.     public static void addRecord(Horse a) throws IOException{
  33.         writeString((int)ShowNameLength, a.getShowName());
  34.         writeString((int)BarnNameLength, a.getBarnName());
  35.         writeString((int)CoatColorLength, a.getCoatColor());
  36.         writeShort((int)AgeLength, a.getAge());
  37.     }
  38.    
  39.     public static void writeString(int maxLength, String A) throws IOException {
  40.         int length;
  41.         if (A.length() < maxLength) {
  42.             length = A.length();
  43.         } else {
  44.             length = maxLength;
  45.             A = A.substring(0, maxLength);
  46.         }
  47.        
  48.         raf.writeShort(length);
  49.         int toBlanc = maxLength - length;
  50.         char pointer;
  51.  
  52.         for (int j = 0; j < A.length(); j++) {
  53.             pointer = A.charAt(j);
  54.             raf.writeChar(pointer);
  55.         }
  56.  
  57.         for (int j = 0; j < (maxLength - A.length()); j++) {
  58.             raf.writeChar(' ');
  59.         }
  60.     }    
  61.    
  62.     public static void writeShort(int maxLength, short a) throws IOException {
  63.         int length;
  64.         String A = a + "";
  65.         if (A.length() < maxLength) {
  66.             length = A.length();
  67.         } else {
  68.             length = maxLength;
  69.             A = A.substring(0, maxLength);
  70.         }
  71.        
  72.         int toBlanc = maxLength - length;
  73.         char pointer;
  74.  
  75.         for (int j = 0; j < A.length(); j++) {
  76.             pointer = A.charAt(j);
  77.             raf.writeChar(pointer);
  78.         }
  79.  
  80.         for (int j = 0; j < (maxLength - A.length()); j++) {
  81.             raf.writeChar(' ');
  82.         }
  83.     }  
  84.    
  85.     public static Object[] readNextRecord(int recordNumber) throws IOException {
  86.         ID++;
  87.         raf.seek(recordNumber * RecordLength);
  88.         Object[] horse = {ID,
  89.                 getString((int)ShowNameLength),
  90.                 getString((int)BarnNameLength),
  91.                 getString((int)CoatColorLength),
  92.                 getShort()} ;
  93.                 return horse;
  94.     }
  95.    
  96.     public static void incrementID(){
  97.         ID++;
  98.     }
  99.    
  100.     public static int getID(){
  101.         return ID;
  102.     }
  103.    
  104.     public static String getString(int maxLength) throws IOException{
  105.         String concat = "";
  106.         int length = raf.readShort();
  107.         //raf.skipBytes(2);
  108.         for(int j = 0 ; j<length; j++){
  109.                 concat += raf.readChar();
  110.         }
  111.         for(int i=length; i<maxLength; i++){
  112.             raf.readChar();
  113.         }
  114.         return concat;
  115.     }
  116.    
  117.     public static short getShort() throws IOException{
  118.         return raf.readShort();
  119.     }
  120.    
  121.     public static long getSize(){
  122.         long length = 0;
  123.         try {
  124.             length = raf.length();
  125.         } catch (IOException ex) {
  126.             Logger.getLogger(BinaryFile.class.getName()).log(Level.SEVERE, null, ex);
  127.         }
  128.         finally{
  129.             return length;
  130.         }
  131.     }
  132.    
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement