Potrzebuję napisać prostą metodę do wczytywania hasła z pliku. Ścieżka do pliku jest poprawna i plik istnieje, nie wiem czemu obiekt scanner zamiast linijek z pliku wczytuje jego adres?
private static String readPasswordFile(String host, String port, String username, String database) {
String password = null;
String line = null;
String appDataRoot = System.getenv("APPDATA");
String passFileRoot = appDataRoot + "\\postgresql\\pgpass.conf";
Scanner file = new Scanner(passFileRoot);
while(file.hasNextLine()){
line = file.nextLine();
if (line.matches(host + ":" + port + ":(.*):" + username + ":(.*)")){
if (line.matches("(.*):(.*):*:(.*):(.*)")) {
password = line.replaceFirst("(.*):(.*):*:(.*):", "");
}
if (line.matches("(.*):(.*):"+ database +":(.*):(.*)")) {
password = line.replaceFirst("(.*):(.*):*:(.*):", "");
}
}
}
file.close();
System.out.println(appDataRoot);
return password;
}