First of all, I'm not a pro by any means so my code is probably terrible, but it's a hobby project just for fun.
I'm trying to parse an M3U playlist file to extract its contents. Currently, if the file is UTF-8, it loads fine. But if it's ASCII, a MalformedInputException is thrown. So I came up with the idea of catching the MalformedInputException and retrying with ISO-8859-1. The code sample is below and it works fine, but I think it looks a bit messy having a try-catch inside another. Is there a better way of doing this? If I only use ISO-8859-1, the formatting of international characters is garbled of course, so it's not something I'd exclusively use.
List<String> pls;
try {
pls = Files.readAllLines(fd, StandardCharsets.UTF_8);
}
catch (MalformedInputException mie){
// Retry using ISO-8859-1
try {
pls = Files.readAllLines(fd, StandardCharsets.ISO_8859_1);
}
catch (IOException ioe) {
// File is unreadable, so stop
System.err.println(ioe);
return false;
}
}
catch (IOException ex) {
// File is unreadable, so stop
System.err.println(ex);
return false;
}