package oop.ExceptionHandling;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class CheckedException {

	public static void main(String[] args) {
		readFile("D:\\Test.txt");

	}
	
	static void readFile(String filePath) {
		FileReader reader = null;
		// hier der Compiler zeigt mir eine Error und zwingt micht eine try-catch Block zu erstellen
		try {
			reader = new FileReader(filePath);// resource
			System.out.println("File Inhalt: ");
			
			int r = 0;
			while((r = reader.read()) != -1) {
				System.out.print((char) r);
			}
		}catch(FileNotFoundException e) {
			
			System.out.println(e);
			
		}catch(IOException e) {
			System.out.println(e);
		}finally {
			try {
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	// try() mit resources, hier benötigt man finally block nicht
	static void readFile2(String filePath) {
		try (FileReader reader = new FileReader(filePath);) {
			System.out.println("File Inhalt: ");
			
			int r = 0;
			while((r = reader.read()) != -1) {
				System.out.print((char) r);
			}
		}catch(FileNotFoundException e) {
			
			System.out.println(e);
			
		}catch(IOException e) {
			System.out.println(e);
		}
	}
	
	
	
	// try() mit mehrere resources, hier benötigt man auch finally block nicht
		static void readFile3(String filePath) {
			try (FileReader reader = new FileReader(filePath); FileReader reader2 = new FileReader(filePath)) {
				System.out.println("File Inhalt: ");
				
				int r = 0;
				while((r = reader.read()) != -1) {
					System.out.print((char) r);
				}
			}catch(FileNotFoundException e) {
				
				System.out.println(e);
				
			}catch(IOException e) {
				System.out.println(e);
			}
		}

}
