Hey,
Ich möchte eine Datei mit dem Namen "Highscore" anlegen. Jedoch muss der Wert "gewonnen" bzw. "verloren" nach jedem Spiel geändert werden. Nun leider verstehe ich momentan einfach nicht wieso mein Sourcecode nicht klappt.
Ich bedanke mich schon einmal für jegliche Hilfe!
Datenordnung innerhalb der Datei:
Name(Win|Lose)
Max(0|1)
Peter(1|0)
...
Strukogramm:
Die Klasse zum auslesen und schreiben der Datei.
Ich möchte eine Datei mit dem Namen "Highscore" anlegen. Jedoch muss der Wert "gewonnen" bzw. "verloren" nach jedem Spiel geändert werden. Nun leider verstehe ich momentan einfach nicht wieso mein Sourcecode nicht klappt.
Ich bedanke mich schon einmal für jegliche Hilfe!
Datenordnung innerhalb der Datei:
Name(Win|Lose)
Max(0|1)
Peter(1|0)
...
Strukogramm:
Die Klasse zum auslesen und schreiben der Datei.
Java:
package common;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
public class FileHighscore {
private String filedir;
//variables for name, win & loose (only 255!)
private String name[] = new String[255];
private String win[] = new String[255];
private String lose[] = new String[255];
//all different value separators
private final int POINT_SEP_A = 40; // ASCII for (
private final int POINT_SEP_B = 41; // ASCII for )
private final int DATA_SEP = 124; // ASCII for |
private final int ENTER = 13; // ASCII for enter
private int handle = -1; //it shows the current array
public FileHighscore(String hFileDir) {
filedir = hFileDir;
}//end Constructor
public void read(){
final int LINEFEED = 10; // enter + line feed is new Line
final int EOF = -1; // end of file
int dmy = -2; // to read the file
String dmyStr = ""; // collect the letters of a value
FileInputStream fis = null; // file handle
boolean ready = false; // loop control variable
int counter = 1;
//open the file:
//==============
try{
fis = new FileInputStream(filedir);
}catch(FileNotFoundException ex){
System.out.println("ERROR 1: File not found => PRG-Stop");
}catch(Exception ex){
System.out.println("ERROR 2: An unexpected error occurred => PRG-Stop: \n" + ex.toString());
}//try
//File read out:
//==============
do{
try{
dmy = fis.read();
}catch(Exception ex){
System.out.println("ERROR 3: Unexpected error while read => PRG-Stop:\n"+ ex.toString() );
System.exit(0);
}//try catch
if(dmy == POINT_SEP_A || dmy == DATA_SEP || dmy == POINT_SEP_B){
//store dmyStr into his variable
if(handle <= 254){
switch(counter){
case 1: name[++handle] = dmyStr; counter++; break;
case 2: win[handle] = dmyStr; counter++; break;
case 3: lose[handle] = dmyStr; counter = 1; break;
}//switch
System.out.println(name[handle] + win[handle] + lose[handle]);
dmyStr = "";//reset dmyStr
}
}else if(dmy == ENTER || dmy == LINEFEED){
//doing nothing
}else if(dmy == EOF){
ready = true;//exit loop
}else{
//complete the value
dmyStr += (char)dmy;
}//if
}while(ready == false);
}//end read (method)
public void write(String hName, boolean hWin){
FileWriter fwriter;
BufferedWriter out;
String inhalt = "";
read();
clearData();
try{
fwriter = new FileWriter(filedir, true);
out = new BufferedWriter(fwriter);
if(getHandle() != -1){
for(int i=0; i<=getHandle(); i++){
inhalt = "";
if(name[i].equals(hName)){
inhalt = hName + "(1|1)";//overwrite
}else{
inhalt = name[i] + (char)POINT_SEP_A + win[i] + (char)DATA_SEP + lose[i] + (char)POINT_SEP_B;
}//if
out.write(inhalt);
out.newLine();
}//for
}else{
inhalt = hName + (char)POINT_SEP_A + "00" + (char)DATA_SEP + "00" + (char)POINT_SEP_B;
out.write(inhalt);
out.newLine();
}//if
out.close();
}catch(FileNotFoundException ex){
System.out.println("ERROR 4: File not found => PRG-Stop");
}catch(Exception ex){
System.out.println("ERROR 5: An unexpected error occurred => PRG-Stop: \n" + ex.toString());
}//try
/*
// inhalt = hName + (char)POINT_SEP_A + hWin + (char)DATA_SEP + hLose + (char)POINT_SEP_B;
//open the file and write into it:
//================================
try{
fwriter = new FileWriter(filedir, true);
out = new BufferedWriter(fwriter);
if(getHandle() > 0){
for(int i=0; i<getHandle()-1; i++){
inhalt = "";
if(name[i].equals(hName)){
inhalt = hName + "(0|10)";
}else{
inhalt = hName + (char)POINT_SEP_A + hWin + (char)DATA_SEP + "lose" + (char)POINT_SEP_B;
}//if
System.out.println("Handle: " + handle + " - "+ getHandle() + " - i: " + i);
out.write(inhalt);
out.newLine();
}//for
}else{
inhalt = hName + (char)POINT_SEP_A + hWin + (char)DATA_SEP + "lose" + (char)POINT_SEP_B;
out.write(inhalt);
out.newLine();
}//if
out.close();
}catch(FileNotFoundException ex){
System.out.println("ERROR 4: File not found => PRG-Stop");
}catch(Exception ex){
System.out.println("ERROR 5: An unexpected error occurred => PRG-Stop: \n" + ex.toString());
}//try*/
}//end write (method)
public String getName(int hLine){
if(handle == 0){//this checks if there is any data inside the file
return("no data inside the file");
}else{
return name[hLine-1];
}
}//end getName
public String getWin(int hLine){
if(handle == 0){//this checks if there is any data inside the file
return("no data inside the file");
}else{
return win[hLine-1];
}
}//end getWin
public String getLose(int hLine){
if(handle == 0){//this checks if there is any data inside the file
return("no data inside the file");
}else{
return lose[hLine-1];
}
}//end getLoose
public int getHandle(){
return handle;
}//end getHandle
public void clearData(){
FileWriter fwriter;
BufferedWriter out;
//open the file and clears it:
//================================
try{
fwriter = new FileWriter(filedir, false);
out = new BufferedWriter(fwriter);
//write new (and old) things into file
/*for(int i=1; i<=handle; i++){
inhalt += "\n";
inhalt += getName(i) + (char)POINT_SEP_A + getWin(i) + (char)DATA_SEP + getLoose(i) + (char)POINT_SEP_B;//old data
}//for*/
out.write("");
out.close();
}catch(FileNotFoundException ex){
System.out.println("ERROR 4: File not found => PRG-Stop");
}catch(Exception ex){
System.out.println("ERROR 5: An unexpected error occurred => PRG-Stop: \n" + ex.toString());
}//try
}//end clearData (method)
}//end FileHighscore (class)