Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. package memoryManagament;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.util.Scanner;
  8.  
  9. public class ExchangeFile {
  10. File exchange = new File("exchangefile.txt");
  11.  
  12. public ExchangeFile() {
  13. try {
  14. if (this.exchange.exists()) {
  15. this.exchange.delete();
  16. this.exchange.createNewFile();
  17. } else
  18. this.exchange.createNewFile();
  19. } catch (IOException e) {
  20. System.out.println("Blad zapisu do pliku");
  21. }
  22. }
  23.  
  24. // ladowanie kodu procesu do pliku wymiany
  25. public PageTable loadProcessData(int process_number, String fileName) {
  26. try {
  27. // odczytanie danych z pliku
  28. File process = new File(fileName);
  29. Scanner read = new Scanner(process);
  30. int pageIndex = 0;
  31.  
  32. String processData = new String("");
  33. // wczytanie danych do stringa
  34. while (read.hasNextLine()) {
  35. processData += read.nextLine();
  36. }
  37.  
  38. read.close();
  39. // dopelnienie zeby liczba znakow byla podzielna przez 16
  40. while (processData.length() % 16 != 0) {
  41. processData += " ";
  42. }
  43.  
  44. File kopia = new File("exchangefile_copy.txt");
  45. PrintWriter push = new PrintWriter(kopia);
  46.  
  47. // zapis danych do pliku wymiany
  48. for (int i = 0; i < processData.length() / 16; i++) {
  49. // System.out.println(processData.substring(pageIndex*16,
  50. // pageIndex*16+16));
  51. // zapisanie nr procesu i nr strony programu
  52. push.println(process_number + " " + pageIndex);
  53.  
  54. // zapisanie strony
  55. push.println(processData.substring(pageIndex * 16, pageIndex * 16 + 16));
  56. pageIndex++;
  57. }
  58.  
  59. // przepisanie tego co było
  60. String dane = "";
  61. Scanner scan = new Scanner(this.exchange);
  62. while (scan.hasNextLine()) {
  63. push.println(scan.nextLine());
  64. }
  65. push.close();
  66. scan.close();
  67.  
  68. this.exchange.delete();
  69. kopia.renameTo(this.exchange);
  70.  
  71. PageTable ret = new PageTable(processData.length(), process_number);
  72. return ret;
  73.  
  74. } catch (FileNotFoundException e) {
  75. System.out.println("Blad: Plik nie istnieje");
  76. } catch (IOException e) {
  77. System.out.println("Blad zapisu do pliku");
  78. }
  79. PageTable ret = null;
  80. return ret;
  81.  
  82. }
  83.  
  84. public void deleteProcessData(int process_number) {
  85. try {
  86. Scanner read = new Scanner(this.exchange);
  87. File newfile = new File("exchangefile_copy.txt");
  88. PrintWriter push = new PrintWriter(newfile);
  89.  
  90. while (read.hasNextLine()) {
  91. int procNum = read.nextInt();
  92. // System.out.println(procNum);
  93. int pageNum = read.nextInt();
  94. // System.out.println(pageNum);
  95. read.nextLine();
  96. String chc = read.nextLine();
  97. // System.out.println(chc);
  98. if (process_number != procNum) {
  99.  
  100. push.println(procNum + " " + pageNum);
  101. push.println(chc);
  102. }
  103.  
  104. }
  105. read.close();
  106. push.close();
  107.  
  108. this.exchange.delete();
  109. newfile.renameTo(this.exchange);
  110.  
  111. } catch (FileNotFoundException e) {
  112. // TODO Auto-generated catch block
  113. e.printStackTrace();
  114. }
  115.  
  116. }
  117.  
  118. // dostan ramke o danym nr procesu i ramki
  119. public String getFrame(int processNumber, int pageNumber) {
  120. try {
  121. Scanner search = new Scanner(exchange);
  122. String check = null;
  123. int procNr = -1, pageNr = -1;
  124. boolean odczytanoPoprawnie = false;
  125. while (search.hasNextLine()) {
  126. check = search.nextLine();
  127. if (check.length() != 16) {
  128. procNr = Integer.parseInt(check.substring(0, 1));
  129. pageNr = Integer.parseInt(check.substring(2, 3));
  130. if (processNumber == procNr && pageNumber == pageNr) {
  131. check = search.nextLine();
  132. odczytanoPoprawnie = true;
  133. break;
  134. }
  135. }
  136.  
  137. }
  138. if (odczytanoPoprawnie)
  139. return check;
  140. else
  141. return null;
  142.  
  143. } catch (FileNotFoundException e) {
  144. System.out.println("NIe ma pliku");
  145. return null;
  146. }
  147. }
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement