I have a basic class FileOutput that will write to a textfile HighScores. I also have a class fileReader that will print out what is written in the textfile. Is there a way to read a line of the textfile HighScores and save it as a String variable? I eventually want to be able to keep track of the top 5 HighScores in a game so I will need a way to compare the latest score to those in the top 5. Here is my code so far:
import java.util.Scanner;
import java.io.File;
public class FileReader
{
public static void main(String args[]) throws Exception
{
// Read from an already existing text file
File inputFile = new File("./src/HighScores");
Scanner sc = new Scanner(inputFile);
while (sc.hasNext()){
String s = sc.next();
System.out.println(s);
}
}
FileOutput Class:
import java.io.PrintWriter;
import java.io.FileWriter;
import java.util.ArrayList;
public class FileOutput
{
public static void main(String args[]) throws Exception
{
FileWriter outFile = new FileWriter("./src/HighScores");
PrintWriter out = new PrintWriter(outFile);
// Write text to file
out.print("Top Five High Scores");
out.println(50);
out.println(45);
out.println(20);
out.println(10);
out.println(5);
out.close();
}
}