Witam!
Pisałem ostatnio klasę Configuration, która posiada metody zapisujące obiekt tej klasy do pliku oraz pobierające z pliku pola danego obiektu. Niestety, coś nie działa (prawdopodobnie metoda save(). Mógłby ktoś rzucić na to okiem?
package files;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
public class Configuration {
public static void main(String args[]){
(new Configuration()).save("config.appfile");
Configuration con = new Configuration("config.appfile");
con.upload("config.appfile");
con.setDzwieki(25);
con.setHard(true);
con.save("config.appfile");
con.upload("config.appfile");
System.out.println(con.toString());
}
@Override
public String toString() {
return "Configuration [isHard=" + isHard + ", SZEROKOSC=" + SZEROKOSC + ", WYSOKOSC=" + WYSOKOSC + ", glosnosc="
+ glosnosc + ", muzyka=" + muzyka + ", dzwieki=" + dzwieki + ", musicon=" + musicon + ", soundon="
+ soundon + ", keyUP=" + keyUP + ", keyDOWN=" + keyDOWN + ", keyLEFT=" + keyLEFT + ", keyRIGHT="
+ keyRIGHT + "]";
}
Configuration(){}
//pola:
//rozgrywka
private boolean isHard;
//grafika
private int SZEROKOSC, WYSOKOSC;
//dźwięk
private int glosnosc, muzyka, dzwieki;
private boolean musicon, soundon;
//sterowanie
private int keyUP, keyDOWN, keyLEFT, keyRIGHT;
//metody
public Configuration(String src){
upload(src);
}
public boolean upload(String src){
File plik = new File(src);
if(plik.exists()&&plik.isFile()){
RandomAccessFile strumien = null;
try {strumien = new RandomAccessFile(plik, "rw");} catch (FileNotFoundException e) {e.printStackTrace();}
read(strumien);
try {strumien.close();} catch (IOException e) {e.printStackTrace();}
return true;
}
else{
return false;}
}
private void read(RandomAccessFile strumien){
byte[] bajty = new byte[9*32+3];
try {strumien.readFully(bajty);} catch (IOException e) {e.printStackTrace();}
int n=0;
//odczyt:
//rozgrywka
this.isHard = bajty[n]!=0; n++;
//grafika
this.SZEROKOSC = getInt(bajty, n); n+=32;
this.WYSOKOSC = getInt(bajty, n); n+=32;
//dzwiek
this.glosnosc = getInt(bajty, n); n+=32;
this.muzyka = getInt(bajty, n); n+=32;
this.dzwieki = getInt(bajty, n); n+=32;
this.musicon = bajty[n]!=0; n++;
this.soundon = bajty[n]!=0; n++;
//sterowanie
this.keyUP = getInt(bajty, n); n+=32;
this.keyDOWN = getInt(bajty, n); n+=32;
this.keyLEFT = getInt(bajty, n); n+=32;
this.keyRIGHT = getInt(bajty, n); n+=32;
}
private int getInt(byte[] bajty, int n){
int x=0;
for(int i=0; i<32; i++){
x*=2;
x+=bajty[n+1];
}
return x;
}
public void save(String src){
File plik = new File(src);
if(plik.exists()==false||plik.isFile()==false){
try {
plik.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
RandomAccessFile strumien;
try {
strumien = new RandomAccessFile(plik, "rw");
byte[] bajty = new byte[9*32+3];
int n=0;
//zapis:
//rozgrywka
if(this.isHard){bajty[n] = 1;}else{bajty[n] = 0;}
n++;
//grafika
bajty = setInt(bajty, this.SZEROKOSC, n); n+=32;
bajty = setInt(bajty, this.WYSOKOSC, n); n+=32;
//dzwiek
bajty = setInt(bajty, this.glosnosc, n); n+=32;
bajty = setInt(bajty, this.muzyka, n); n+=32;
bajty = setInt(bajty, this.dzwieki, n); n+=32;
if(this.musicon){bajty[n] = 1;}else{bajty[n] = 0;}
n++;
if(this.soundon){bajty[n] = 1;}else{bajty[n] = 0;}
n++;
//sterowanie
bajty = setInt(bajty, this.keyUP, n); n+=32;
bajty = setInt(bajty, this.keyDOWN, n); n+=32;
bajty = setInt(bajty, this.keyLEFT, n); n+=32;
bajty = setInt(bajty, this.keyRIGHT, n); n+=32;
strumien.write(bajty);
strumien.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private byte[] setInt(byte[] bajty, int x, int n){
for(int i=0; i<32; i++){ bajty[n+31-i] = (byte) (x%2);
x/=2;
}
return bajty;
}
//GETTER/SETTER AREA
public boolean isHard() {
return isHard;
}
public void setHard(boolean isHard) {
this.isHard = isHard;
}
public int getSZEROKOSC() {
return SZEROKOSC;
}
public void setSZEROKOSC(int sZEROKOSC) {
SZEROKOSC = sZEROKOSC;
}
public int getWYSOKOSC() {
return WYSOKOSC;
}
public void setWYSOKOSC(int wYSOKOSC) {
WYSOKOSC = wYSOKOSC;
}
public int getGlosnosc() {
return glosnosc;
}
public void setGlosnosc(int glosnosc) {
this.glosnosc = glosnosc;
}
public int getMuzyka() {
return muzyka;
}
public void setMuzyka(int muzyka) {
this.muzyka = muzyka;
}
public int getDzwieki() {
return dzwieki;
}
public void setDzwieki(int dzwieki) {
this.dzwieki = dzwieki;
}
public boolean isMusicon() {
return musicon;
}
public void setMusicon(boolean musicon) {
this.musicon = musicon;
}
public boolean isSoundon() {
return soundon;
}
public void setSoundon(boolean soundon) {
this.soundon = soundon;
}
public int getKeyUP() {
return keyUP;
}
public void setKeyUP(int keyUP) {
this.keyUP = keyUP;
}
public int getKeyDOWN() {
return keyDOWN;
}
public void setKeyDOWN(int keyDOWN) {
this.keyDOWN = keyDOWN;
}
public int getKeyLEFT() {
return keyLEFT;
}
public void setKeyLEFT(int keyLEFT) {
this.keyLEFT = keyLEFT;
}
public int getKeyRIGHT() {
return keyRIGHT;
}
public void setKeyRIGHT(int keyRIGHT) {
this.keyRIGHT = keyRIGHT;
}
}