Rozwiązanie zadania z obsługi błędów

package pl.am.errors.lesson12;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FileTest {
    public static void main(String[] args)  {

        try (FileWriter out = new FileWriter("out.txt", true)) {

            out.write("testowa zawartośc pliku\n");

        } catch (IOException e) {
            e.printStackTrace();
        }

        try (Scanner scanner = new Scanner(new File("out.txt"))) {
            while(scanner.hasNext()) {
                System.out.println(scanner.nextLine());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        
    }
}
package pl.am.errors.lesson12;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class FileTest2 {
    public static void main(String[] args)  {

        try (FileWriter out = new FileWriter("out.txt", true);
             Scanner scanner = new Scanner(new File("out.txt"))) {

            out.write("testowa zawartośc pliku\n");
            out.close();

            while(scanner.hasNext()) {
                System.out.println(scanner.nextLine());
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

Dodaj komentarz

Twój adres e-mail nie zostanie opublikowany. Wymagane pola są oznaczone *