Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package horsemanager;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.RandomAccessFile;
- import java.util.ArrayList;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class BinaryFile {
- public static final int RecordLength = 102;
- private static final short ShowNameLength = 25;
- private static final short BarnNameLength = 20;
- private static final short CoatColorLength = 11;
- private static final short AgeLength = 2;
- private static short pointer = 0;
- private static int ID = 0;
- public final static String FILE_NAME = "/Users/hanaezz/Desktop/horseRecords.dat";
- static private RandomAccessFile raf;
- public BinaryFile() throws IOException {
- File f = new File(FILE_NAME);
- if (!f.exists()) {
- f.createNewFile();
- }
- raf = new RandomAccessFile(FILE_NAME, "rw");
- }
- public static void addRecord(Horse a) throws IOException{
- writeString((int)ShowNameLength, a.getShowName());
- writeString((int)BarnNameLength, a.getBarnName());
- writeString((int)CoatColorLength, a.getCoatColor());
- writeShort((int)AgeLength, a.getAge());
- }
- public static void writeString(int maxLength, String A) throws IOException {
- int length;
- if (A.length() < maxLength) {
- length = A.length();
- } else {
- length = maxLength;
- A = A.substring(0, maxLength);
- }
- raf.writeShort(length);
- int toBlanc = maxLength - length;
- char pointer;
- for (int j = 0; j < A.length(); j++) {
- pointer = A.charAt(j);
- raf.writeChar(pointer);
- }
- for (int j = 0; j < (maxLength - A.length()); j++) {
- raf.writeChar(' ');
- }
- }
- public static void writeShort(int maxLength, short a) throws IOException {
- int length;
- String A = a + "";
- if (A.length() < maxLength) {
- length = A.length();
- } else {
- length = maxLength;
- A = A.substring(0, maxLength);
- }
- int toBlanc = maxLength - length;
- char pointer;
- for (int j = 0; j < A.length(); j++) {
- pointer = A.charAt(j);
- raf.writeChar(pointer);
- }
- for (int j = 0; j < (maxLength - A.length()); j++) {
- raf.writeChar(' ');
- }
- }
- public static Object[] readNextRecord(int recordNumber) throws IOException {
- ID++;
- raf.seek(recordNumber * RecordLength);
- Object[] horse = {ID,
- getString((int)ShowNameLength),
- getString((int)BarnNameLength),
- getString((int)CoatColorLength),
- getShort()} ;
- return horse;
- }
- public static void incrementID(){
- ID++;
- }
- public static int getID(){
- return ID;
- }
- public static String getString(int maxLength) throws IOException{
- String concat = "";
- int length = raf.readShort();
- //raf.skipBytes(2);
- for(int j = 0 ; j<length; j++){
- concat += raf.readChar();
- }
- for(int i=length; i<maxLength; i++){
- raf.readChar();
- }
- return concat;
- }
- public static short getShort() throws IOException{
- return raf.readShort();
- }
- public static long getSize(){
- long length = 0;
- try {
- length = raf.length();
- } catch (IOException ex) {
- Logger.getLogger(BinaryFile.class.getName()).log(Level.SEVERE, null, ex);
- }
- finally{
- return length;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement