Pliki, strumienie, wejścia-wyjścia, NIO

0

Hey. Mam problem. Mam plik z rozszerzeniem "conf" i muszę napisać metodę która wykona poniższe 3 punkty. Czy pliki z takim rozszerzeniem wczytuje się w taki sam sposób jak standardowe txt itp, tzn tak jak zrobiłem w poniższym kodzie? Oraz punkt 2 i 3 zrobić za pomocą instrukcji try/catch ??

  1. read credentials from the file "res/zadanie1/skynet.conf"
  2. print "INFO System reconfigured..." if correctly read
  3. print "INFO No valid conf data" if could not read credentials
 private void readCredentials() {
		File logoFile = new File("skynet.conf");
		
		try (BufferedReader reader = Files.newBufferedReader(logoFile.toPath())) {
			String line = null;
			while((line = reader.readLine()) !=null) {
				System.out.println(line);
			}
		}catch(IOException x) {
			System.err.format("IOException: %s", x);
		}
	}
0

Musisz zobaczyć jak wygląda poprawny plik. Inaczej nie da rady. No bo jak zweryfikować dane bez kryterium poprawności.
Przy okazji może łatwiej będzie wykorzystać: Files.readAllLines(Path)

0

To jest cały mój kod.
Brakuje mi jedynie matody readCredentials(). Plik .conf zawiera napisy Login, Password.


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.nio.file.Files;

public class Skynet {
	
	private String login = "Reese";
	private String password = "#Terminate";
		
	public static void main(String args[]) {
		Skynet skynet = new Skynet();
		skynet.run();
	}
	
	public void run() {
		readCredentials();
		printSkyNetLogo();
		runLoginPanel();
	}
	
	private void runLoginPanel() {
		String readLogin;
		String readPassword;
		boolean credentialsOK;
		do {
			readLogin = getTextFromInput("Login");
			readPassword = getTextFromInput("Password");
			credentialsOK = verifyCredentials(readLogin, readPassword); 
			if (!credentialsOK) {
				System.out.println("Wrong login or password. Try again.");
			}
		} while (!credentialsOK);
		System.out.println();
		System.out.println("Welcome to SKYNET...");
	}
	
	private String getTextFromInput(String label) {
		Scanner in = new Scanner(System.in);
		
		String login, password;
		
		System.out.print("Enter your username: ");
		login = in.nextLine();
		
		System.out.print("Enter your password: ");
		password = in.nextLine(); 
		
		
		return null;
	}
	
	private boolean verifyCredentials(String login, String password) {
		return this.login.equals(login) && this.password.equals(password);
	}
	 
	private void readCredentials() {
		
		// TODO read credentials from the file "..skynet.conf"
		// TODO print "INFO System reconfigured..." if correctly read
		// TODO print "INFO No valid conf data" if could not read credentials
	
	private void printSkyNetLogo() {

		File logoFile = new File("logo.txt");
				
		try (BufferedReader reader = Files.newBufferedReader(logoFile.toPath())) {
			String line = null;
			while((line = reader.readLine()) !=null) {
				System.out.println(line);
			}
		}catch(IOException x) {
			System.err.format("IOException: %s", x);
		}
	}
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1