Ok i have some code which stores a subject, difficulty, correctAnswers, maxQuestions and date into a text file.
Now when i want this file to be read i can display all the information with no problem, but i want it so the newest date is a the top.

Heres my code to read the data:

try
		{
			FileReader fr = new FileReader (file);
			BufferedReader inputFile = new BufferedReader(fr);
			
			line = inputFile.readLine();
			scorePanel.add(new JLabel(line));
			System.out.println(line);
			while (line != null)
			{
				line = inputFile.readLine();
				scorePanel.add(new JLabel(line));
				System.out.println(line);
				
			}
			
			inputFile.close();
			
		}
		catch (FileNotFoundException exception)
		{
			System.out.println("File not found");
		}
		catch (IOException exception)
		{
			System.out.println(exception);
		}

Or is it possible that when you write to the text file you can put the new information above the old one? My current code to write to the file is:

try {
	        BufferedWriter bw = new BufferedWriter(new FileWriter("scores.dat", true));
	        PrintWriter outputFile = new PrintWriter(bw);
			outputFile.print(subject + "  " + difficulty + "  " + correctAnswers + "  " + maxQuestions+"  "+ finalPercentage + "%  "+ date);
			outputFile.println();
			outputFile.close();
	    } catch (IOException e) {
	    }

Hope this makes sense and thank you for any help :)

You need to store your information into an array, sort the array by date, then output the text to the screen.

Since each line contains multiple pieces of info, create a new class and build objects from that and implement a compareTo method for the sorting.

This article has been dead for over six months. Start a new discussion instead.