Advertisement
Guest User

Untitled

a guest
May 5th, 2018
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1. package ćw6;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.FileReader;
  7. import java.io.IOException;
  8. import java.io.PrintStream;
  9. import java.util.HashSet;
  10. import java.util.LinkedList;
  11. import java.util.Scanner;
  12. import java.util.Set;
  13.  
  14. public class File
  15. {
  16.     static Set<String> SetNames = new HashSet<String>();
  17.     static Set<String> SetSurnames = new HashSet<String>();
  18.     static LinkedList<String> ListNames = new LinkedList<String>();
  19.     static LinkedList<String> ListSurnames = new LinkedList<String>();
  20.  
  21.     public static void record() throws IOException
  22.     {
  23.         Scanner scanner = new Scanner(System.in);
  24.         PrintStream namesWriter = new PrintStream(new FileOutputStream("Names.txt", true));
  25.         PrintStream surnamesWriter = new PrintStream(new FileOutputStream("Surnames.txt", true));
  26.  
  27.         System.out.println("Podawaj imiona i nazwiska. Wyraz \"stop\" kończy wpisywanie\n");
  28.         System.out.print("Podaj imię: ");
  29.         String name = scanner.nextLine();
  30.         System.out.print("Podaj nazwisko: ");
  31.         String surname = scanner.nextLine();
  32.         while (((name.equalsIgnoreCase("stop")) || (surname.equalsIgnoreCase("stop"))) == false)
  33.             {
  34.                 namesWriter.println(name);
  35.                 surnamesWriter.println(surname);
  36.                 System.out.print("Podaj imię: ");
  37.                 name = scanner.nextLine();
  38.                 System.out.print("Podaj nazwisko: ");
  39.                 surname = scanner.nextLine();
  40.             }
  41.         System.out.println("\nWpisywanie zakończone!");
  42.         namesWriter.close();
  43.         surnamesWriter.close();
  44.         scanner.close();
  45.     }
  46.  
  47.     public static LinkedList<String> readingNames() throws FileNotFoundException, IOException
  48.     {
  49.         try
  50.             {
  51.                 FileReader readNames = new FileReader("Names.txt");
  52.                 BufferedReader bufferedReaderNames = new BufferedReader(readNames);
  53.                 String line = bufferedReaderNames.readLine();
  54.                 while (line != null)
  55.                     {
  56.                         SetNames.add(line);
  57.                         line = bufferedReaderNames.readLine();
  58.                     }
  59.                 ListNames.addAll(SetNames);
  60.                 bufferedReaderNames.close();
  61.                 readNames.close();
  62.             } catch (FileNotFoundException e)
  63.             {
  64.                 System.err.println("Nie można odnalezć pliku o nazwie \"Names.txt\"");
  65.             } catch (IOException e)
  66.             {
  67.                 System.err.println("Nieznany błąd wejścia typu Input-Output");
  68.             }
  69.         return ListNames;
  70.     }
  71.  
  72.     public static LinkedList<String> readingSurnames() throws FileNotFoundException, IOException
  73.     {
  74.         try
  75.             {
  76.                 FileReader readSurnames = new FileReader("Surnames.txt");
  77.                 BufferedReader bufferedReadSurnames = new BufferedReader(readSurnames);
  78.  
  79.                 String line = bufferedReadSurnames.readLine();
  80.                 while (line != null)
  81.                     {
  82.                         SetSurnames.add(line);
  83.                         line = bufferedReadSurnames.readLine();
  84.                     }
  85.                 ListSurnames.addAll(SetSurnames);
  86.                 bufferedReadSurnames.close();
  87.                 readSurnames.close();
  88.             } catch (FileNotFoundException e)
  89.             {
  90.                 System.err.println("Nie można znalezć pliku o nazwie \"Surnames.txt\"");
  91.             } catch (IOException e)
  92.             {
  93.                 System.err.println("Nieznany błąd wejścia typu Input-Output");
  94.             }
  95.         return ListSurnames;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement