gregdc@webtrace.be před 3 roky
rodič
revize
b05d1aab89

binární
DBServer.db Zobrazit soubor


+ 69
- 0
src/main/java/cc/javastudio/raw/FileHandler.java Zobrazit soubor

@@ -0,0 +1,69 @@
package cc.javastudio.raw;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class FileHandler implements Closeable {
private final int INT_LENGTH = 4;
private final byte BYTE_LENGTH = 1;
private RandomAccessFile dbFile;

public FileHandler(String dbFileName) throws FileNotFoundException {
dbFile = new RandomAccessFile(dbFileName,"rw");
}

public void add(String name, int age, String address, String licencePlate, String desciption) throws IOException {
dbFile.seek(dbFile.length());
int recordLength = INT_LENGTH + name.length() +
INT_LENGTH +
INT_LENGTH + address.length() +
INT_LENGTH + licencePlate.length() +
INT_LENGTH + desciption.length();

dbFile.writeBoolean(false);
dbFile.writeInt(recordLength);
dbFile.writeInt(name.length());
dbFile.write(name.getBytes());
dbFile.writeInt(age);
dbFile.writeInt(address.length());
dbFile.write(address.getBytes());
dbFile.writeInt(licencePlate.length());
dbFile.write(licencePlate.getBytes());
dbFile.writeInt(desciption.length());
dbFile.write(desciption.getBytes());
}

public Person readRow(int rowNUmber) throws IOException {
Person person = new Person();
byte [] rawData = readRawRecord(0);
DataInputStream stream = new DataInputStream(new ByteArrayInputStream(rawData));
byte[] b = new byte[stream.readInt()];
stream.read(b);
person.name = new String(b);

return person;
}

private byte[] readRawRecord(int rowNumber) throws IOException {
int pos = 0;
byte [] data;
dbFile.seek(pos);

if(dbFile.readBoolean()){
data = new byte[0];
}
else {
pos+= BYTE_LENGTH;
dbFile.seek(pos);
int recordLength = dbFile.readInt();
pos += INT_LENGTH;
data = new byte[recordLength];
dbFile.read(data);
}
return data;
}

public void close() throws IOException {
dbFile.close();
}
}

+ 20
- 0
src/main/java/cc/javastudio/raw/Person.java Zobrazit soubor

@@ -0,0 +1,20 @@
package cc.javastudio.raw;

public class Person {
public String name;
public int age;
public String address;
public String licencePlate;
public String Description;

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
", licencePlate='" + licencePlate + '\'' +
", Description='" + Description + '\'' +
'}';
}
}

+ 16
- 0
src/main/java/cc/javastudio/testapp/TestApp.java Zobrazit soubor

@@ -0,0 +1,16 @@
package cc.javastudio.testapp;

import cc.javastudio.raw.FileHandler;

import java.io.FileNotFoundException;
import java.io.IOException;

public class TestApp {
public static void main(String[] args) {
try(FileHandler fh = new FileHandler("DBServer.db")) {
fh.add("John Doe", 44, "New-York", "www-404", "Blue VW Beetle");
} catch (IOException e) {
e.printStackTrace();
}
}
}

Načítá se…
Zrušit
Uložit