Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1.  
  2. import java.io.DataInputStream;
  3. import java.io.DataOutputStream;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9.  
  10. /*
  11. * To change this license header, choose License Headers in Project Properties.
  12. * To change this template file, choose Tools | Templates
  13. * and open the template in the editor.
  14. */
  15. /**
  16. *
  17. * @author aldui
  18. */
  19. public class Jedinacek {
  20.  
  21. private static Jedinacek instance;
  22. int counter;
  23.  
  24. private Jedinacek() {
  25.  
  26. DataInputStream input;
  27. try {
  28. input = new DataInputStream(new FileInputStream("counter.dat"));
  29.  
  30. counter = input.readInt();
  31.  
  32. input.close();
  33. } catch (Exception ex) {
  34. try {
  35. FileOutputStream fos = new FileOutputStream("counter.dat");
  36. DataOutputStream dos = new DataOutputStream(fos);
  37. counter = 0;
  38. dos.writeInt(counter);
  39. dos.close();
  40. } catch (IOException ex1) {
  41. Logger.getLogger(Jedinacek.class.getName()).log(Level.SEVERE, null, ex1);
  42. }
  43. }
  44.  
  45. }
  46.  
  47. public static Jedinacek getInstance() {
  48. if (instance == null) {
  49. instance = new Jedinacek();
  50. }
  51.  
  52. return instance;
  53. }
  54.  
  55. public void addOne() {
  56. counter += 1;
  57. try {
  58. FileOutputStream fos = new FileOutputStream("counter.dat");
  59. DataOutputStream dos = new DataOutputStream(fos);
  60. dos.writeInt(counter);
  61. dos.close();
  62. } catch (IOException ex) {
  63. Logger.getLogger(Jedinacek.class.getName()).log(Level.SEVERE, null, ex);
  64. }
  65. }
  66.  
  67. public int getCounter() {
  68. return counter;
  69. }
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement