Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.EOFException;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Date;
- import data.Highscore;
- import data.HighscoreContainer;
- import data.Spieler;
- public class Stream implements Datenhaltung{
- File file = new File("Highscore.dat");
- @Override
- public HighscoreContainer loadHighscore() {
- HighscoreContainer container = HighscoreContainer.instance();
- if(!file.exists())
- return container;
- try {
- DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
- while(true){
- int length = input.readInt();
- String name = "";
- for(int i=0;i<length;i++)
- name+=input.readChar();
- long zeit = input.readLong();
- int punkte = input.readInt();
- Highscore eintrag = new Highscore(new Spieler(name), new Date(zeit), punkte);
- }
- } catch(EOFException e){
- System.out.println("Highscore geladen!");
- }catch (FileNotFoundException e) {
- e.printStackTrace();
- }catch(IOException e){
- e.printStackTrace();
- }finally{
- return container;
- }
- }
- @Override
- public void updateHighscore() {
- if(!file.exists())
- try {
- file.createNewFile();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- try {
- DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
- for(Highscore highscore: HighscoreContainer.instance()){
- out.writeInt(highscore.getName().length());;
- for(char c: highscore.getName().toCharArray())
- out.writeChar(c);
- out.writeLong(highscore.getDateTime());
- out.writeInt(highscore.getPunkte());
- }
- System.out.println("Highscore saved!");
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch(IOException e){
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment