LukasHuhn

Untitled

Jun 17th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1. package io;
  2.  
  3. import java.io.BufferedInputStream;
  4. import java.io.BufferedOutputStream;
  5. import java.io.DataInputStream;
  6. import java.io.DataOutputStream;
  7. import java.io.EOFException;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileNotFoundException;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.util.Date;
  14.  
  15. import data.Highscore;
  16. import data.HighscoreContainer;
  17. import data.Spieler;
  18.  
  19. public class Stream implements Datenhaltung{
  20.     File file = new File("Highscore.dat");
  21.    
  22.     @Override
  23.     public HighscoreContainer loadHighscore() {
  24.         HighscoreContainer container = HighscoreContainer.instance();
  25.         if(!file.exists())
  26.             return container;
  27.         try {
  28.             DataInputStream input = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
  29.             while(true){
  30.                 int length = input.readInt();
  31.                 String name = "";
  32.                 for(int i=0;i<length;i++)
  33.                     name+=input.readChar();
  34.                 long zeit = input.readLong();
  35.                 int punkte = input.readInt();
  36.                 Highscore eintrag = new Highscore(new Spieler(name), new Date(zeit), punkte);
  37.             }
  38.         } catch(EOFException e){
  39.             System.out.println("Highscore geladen!");
  40.         }catch (FileNotFoundException e) {
  41.             e.printStackTrace();
  42.         }catch(IOException e){
  43.             e.printStackTrace();
  44.         }finally{
  45.             return container;
  46.         }
  47.     }
  48.  
  49.     @Override
  50.     public void updateHighscore() {
  51.         if(!file.exists())
  52.             try {
  53.                 file.createNewFile();
  54.             } catch (IOException e1) {
  55.                 e1.printStackTrace();
  56.             }
  57.        
  58.         try {
  59.             DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
  60.             for(Highscore highscore: HighscoreContainer.instance()){
  61.                 out.writeInt(highscore.getName().length());;
  62.                 for(char c: highscore.getName().toCharArray())
  63.                     out.writeChar(c);
  64.                 out.writeLong(highscore.getDateTime());
  65.                 out.writeInt(highscore.getPunkte());
  66.             }
  67.             System.out.println("Highscore saved!");
  68.         } catch (FileNotFoundException e) {
  69.             e.printStackTrace();
  70.         } catch(IOException e){
  71.             e.printStackTrace();
  72.         }
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment