Comments on FileReading

This commit is contained in:
Zach S-B 2021-08-15 19:17:49 +10:00
parent f152936d15
commit ab4ee99b78

View File

@ -3,7 +3,7 @@ import java.io.*;
public class Scheduler { public class Scheduler {
//Address of File name relative to code. Replace with request for filename? //Address of File name relative to code. Replace with request for filename?
String fileName = "datafiles/datafile2.txt"; String fileName = "datafiles/datafile1.txt";
//Initialise Processes. Assumes max of 10. //Initialise Processes. Assumes max of 10.
Process[] p = new Process[10]; Process[] p = new Process[10];
@ -29,10 +29,32 @@ public class Scheduler {
readFile(fileName); //Read the file readFile(fileName); //Read the file
//Test Print //Test Print
System.out.println("Begin Process List:");
for (int i = 0; i <= processCount; i++)
{
System.out.print("ID: ");
System.out.println(p[i].getId());
System.out.print("Arrive: ");
System.out.println(p[i].getArrive());
System.out.print("ExecSize: ");
System.out.println(p[i].getExecSize());
System.out.print("Tickets: ");
System.out.println(p[i].getTickets());
System.out.println();
}
System.out.println("End Process List.");
System.out.println("Begin Random:");
for (int i = 0; i <= randomNumLength; i++) for (int i = 0; i <= randomNumLength; i++)
{ {
System.out.println(randomNum[i]); System.out.println(randomNum[i]);
} }
System.out.println("End Random.");
} }
/** /**
@ -50,24 +72,27 @@ public class Scheduler {
fileReader = new Scanner(dataSet); fileReader = new Scanner(dataSet);
System.out.println("File Opened."); System.out.println("File Opened.");
//Progress through each line in the text file
while(fileReader.hasNextLine()){ while(fileReader.hasNextLine()){
String line = fileReader.nextLine(); String line = fileReader.nextLine();
//If the line has "EOF" written, stop reading lines.
if(line.equals("EOF")) if(line.equals("EOF"))
{ {
System.out.println("End of File Reached"); System.out.println("End of File Reached");
break; break;
} }
//If it is a new proccess //If the Line starts with "ID:", create a new process
if(line.startsWith("ID:")) if(line.startsWith("ID:"))
{ {
//Create new proccess //Create new proccess
processCount++; processCount++; //Increment the number of processes.
p[processCount] = new Process(); p[processCount] = new Process();
p[processCount].setId(line.substring(4)); p[processCount].setId(line.substring(4)); //Set the id of the proccess to whatever remains of the line after the "ID: "
//As we've read in the ID, we shall assume the following lines (until "END") are all part of the process definition.
while(fileReader.hasNextLine()) while(fileReader.hasNextLine())
{ {
line = fileReader.nextLine(); line = fileReader.nextLine();
@ -79,7 +104,6 @@ public class Scheduler {
else if(line.startsWith("Arrive:")) else if(line.startsWith("Arrive:"))
{ {
p[processCount].setArrive(Integer.parseInt(line.substring(8))); p[processCount].setArrive(Integer.parseInt(line.substring(8)));
} }
@ -93,9 +117,17 @@ public class Scheduler {
{ {
p[processCount].setTickets(Integer.parseInt(line.substring(9))); p[processCount].setTickets(Integer.parseInt(line.substring(9)));
} }
else
{
//Delete process?
}
} }
} }
//If we read the line "BEGINRANDOM", we've started the random numbers section. Until the "ENDRANDOM" line,
//we'll just keep reading in each number (and it is assumed each line is a single number) and adding it
//to the random number array.
else if(line.equals("BEGINRANDOM")) else if(line.equals("BEGINRANDOM"))
{ {
while(fileReader.hasNextLine()) while(fileReader.hasNextLine())
@ -117,12 +149,10 @@ public class Scheduler {
fileReader.close(); fileReader.close();
} }
//If the file isn't found, through a hissy fit. //If the file isn't found, throw a hissy fit.
catch (FileNotFoundException e) { catch (FileNotFoundException e) {
System.out.println("File not found."); System.out.println("File not found.");
//e.printStackTrace(); //Optional, shows more data. //e.printStackTrace(); //Optional, shows more data.
} }
} }
} }