Я пытаюсь запустить следующий код, используя пакетный файл URL-адресов, хотя URL-адрес не является нулевым.Он работает именно так, как я хочу, однако последняя строка вывода показывает:
Исключение искаженного URL: 'null' не является допустимым вводом
Пакетная программа успешно завершена
Я хочу, чтобы программа остановилась прямо перед тем, как она фактически прочитает ноль.Есть какие-нибудь предложения?
(Кроме того, я все еще учусь задавать вопросы, чтобы люди не сердились на меня, поэтому, если я смогу улучшить свой вопрос, просто дайте мне знать, вместо того, чтобы голосовать вниз. Спасибо.)
private static String getBatchUrlContents(String filePath) {
logger.debug(">>getBatchUrlContents()");
String theUrl = "";
try {
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedUrlReader = new BufferedReader(fileReader);
do {
try {
theUrl = bufferedUrlReader.readLine();
URL url = new URL(theUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
String contentType = urlConnection.getContentType();
logger.info("Content Type: {}", contentType);
int statusCode = urlConnection.getResponseCode();
logger.info("Status Code: {}", statusCode);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
List<String> contents = new ArrayList<String>();
while ((line = bufferedReader.readLine()) != null) {
contents.add(line);
}
bufferedReader.close();
String[] contentArray = contents.toArray(new String[contents.size()]);
System.out.println("\n ~~~~~~~NEW URL~~~~~~~ \n" + theUrl);
for (String x :contentArray)
System.out.println(x);
String firstLine = contentArray[0];
if (firstLine.equals("#EXTM3U")) {
logger.info("First line is validated: {}", firstLine);
System.out.println("First line of content is validated: " + firstLine);
} else {
logger.error("Error: First line not valid: {}", firstLine);
System.out.println("Error: First line reads: '" + firstLine + "'\n"
+ "Should read: '#EXTM3U'");
}
} catch(Exception e) {
if (e instanceof MalformedURLException) {
logger.error("Malformed URL Exception: {}", e.toString());
System.out.println("Malformed URL Exception: '" + theUrl + "' is not a valid input");
} else if (e instanceof FileNotFoundException) {
logger.error("404: File Not Found Exception: {}", e.toString());
System.out.println("Unable to open URL: " + theUrl);
} else if (e instanceof IOException) {
logger.error("IO Exception: {}", e.toString());
System.out.println("Error reading file: " + theUrl);
} else {
logger.error("Exception: {}", e.toString());
System.out.println("Exception Error - Unable to read or open: " + theUrl);
}
}
} while (theUrl != null);
bufferedUrlReader.close();
} catch (FileNotFoundException ex) {
System.out.println("Unable to open file '" + filePath + "'");
System.exit(2);
} catch (IOException ex) {
System.out.println("Error reading file '" + filePath + "'");
System.exit(3);
}
logger.debug("<<getUrlContents()");
return "Batch Program Completed Successfully";
}