Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package memoryManagament;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Scanner;
- public class ExchangeFile {
- File exchange = new File("exchangefile.txt");
- public ExchangeFile() {
- try {
- if (this.exchange.exists()) {
- this.exchange.delete();
- this.exchange.createNewFile();
- } else
- this.exchange.createNewFile();
- } catch (IOException e) {
- System.out.println("Blad zapisu do pliku");
- }
- }
- // ladowanie kodu procesu do pliku wymiany
- public PageTable loadProcessData(int process_number, String fileName) {
- try {
- // odczytanie danych z pliku
- File process = new File(fileName);
- Scanner read = new Scanner(process);
- int pageIndex = 0;
- String processData = new String("");
- // wczytanie danych do stringa
- while (read.hasNextLine()) {
- processData += read.nextLine();
- }
- read.close();
- // dopelnienie zeby liczba znakow byla podzielna przez 16
- while (processData.length() % 16 != 0) {
- processData += " ";
- }
- File kopia = new File("exchangefile_copy.txt");
- PrintWriter push = new PrintWriter(kopia);
- // zapis danych do pliku wymiany
- for (int i = 0; i < processData.length() / 16; i++) {
- // System.out.println(processData.substring(pageIndex*16,
- // pageIndex*16+16));
- // zapisanie nr procesu i nr strony programu
- push.println(process_number + " " + pageIndex);
- // zapisanie strony
- push.println(processData.substring(pageIndex * 16, pageIndex * 16 + 16));
- pageIndex++;
- }
- // przepisanie tego co było
- String dane = "";
- Scanner scan = new Scanner(this.exchange);
- while (scan.hasNextLine()) {
- push.println(scan.nextLine());
- }
- push.close();
- scan.close();
- this.exchange.delete();
- kopia.renameTo(this.exchange);
- PageTable ret = new PageTable(processData.length(), process_number);
- return ret;
- } catch (FileNotFoundException e) {
- System.out.println("Blad: Plik nie istnieje");
- } catch (IOException e) {
- System.out.println("Blad zapisu do pliku");
- }
- PageTable ret = null;
- return ret;
- }
- public void deleteProcessData(int process_number) {
- try {
- Scanner read = new Scanner(this.exchange);
- File newfile = new File("exchangefile_copy.txt");
- PrintWriter push = new PrintWriter(newfile);
- while (read.hasNextLine()) {
- int procNum = read.nextInt();
- // System.out.println(procNum);
- int pageNum = read.nextInt();
- // System.out.println(pageNum);
- read.nextLine();
- String chc = read.nextLine();
- // System.out.println(chc);
- if (process_number != procNum) {
- push.println(procNum + " " + pageNum);
- push.println(chc);
- }
- }
- read.close();
- push.close();
- this.exchange.delete();
- newfile.renameTo(this.exchange);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- // dostan ramke o danym nr procesu i ramki
- public String getFrame(int processNumber, int pageNumber) {
- try {
- Scanner search = new Scanner(exchange);
- String check = null;
- int procNr = -1, pageNr = -1;
- boolean odczytanoPoprawnie = false;
- while (search.hasNextLine()) {
- check = search.nextLine();
- if (check.length() != 16) {
- procNr = Integer.parseInt(check.substring(0, 1));
- pageNr = Integer.parseInt(check.substring(2, 3));
- if (processNumber == procNr && pageNumber == pageNr) {
- check = search.nextLine();
- odczytanoPoprawnie = true;
- break;
- }
- }
- }
- if (odczytanoPoprawnie)
- return check;
- else
- return null;
- } catch (FileNotFoundException e) {
- System.out.println("NIe ma pliku");
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement