I have tried multiple solutions suggested by other threads on StackOverflow, but I haven't found a solution. I'm trying to read a .txt file and sort the content from largest to smallest, using Java. This is what my .txt file contains:
16° C
15° C
18° C
13° C
17° C
19° C
21° C
20° C
16° C
Here is what I've tried:
import java.io.*;
import java.util.*;
public class temperatur {
public static void main(String[] args) throws IOException {
BufferedReader reader = null;
PrintWriter outputStream = null;
ArrayList<String> rows = new ArrayList<String>();
try {
reader = new BufferedReader(new FileReader("temp.txt"));
outputStream = new PrintWriter(new FileWriter("tempout.txt"));
String file;
while ((file = reader .readLine()) != null) {
rows.add(file);
}
Collections.sort(rows);
String[] strArr= rows.toArray(new String[0]);
for (String cur : strArr)
outputStream.println(cur);
} finally {
if (reader != null) {
inputStream.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
}