Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.HashSet;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.  
  9.     public static HashSet<Integer> ReadFromFileFirst() throws IOException {
  10.         String fileName = "C:\\Users\\Asus\\Desktop\\Laba3_2.txt";
  11.         FileInputStream file = new FileInputStream(fileName);
  12.         HashSet<Integer> FirstSet = new HashSet<>();
  13.         while (file.available()>0) {
  14.             FirstSet.add(file.read());
  15.         }
  16.         System.out.println(FirstSet);
  17.         file.close();
  18.         return FirstSet;
  19.     }
  20.  
  21.     public static HashSet<Integer> ReadFromFileSecond() throws IOException {
  22.         String fileName = "C:\\Users\\Asus\\Desktop\\Laba3_2(X2).txt";
  23.         FileInputStream file = new FileInputStream(fileName);
  24.         HashSet<Integer> SecondSet = new HashSet<>();
  25.         while (file.available()>0) {
  26.             SecondSet.add(file.read());
  27.         }
  28.         System.out.println(SecondSet);
  29.         file.close();
  30.         return SecondSet;
  31.     }
  32.  
  33.     public static HashSet UnionOfSets(HashSet<Integer> FirstSet, HashSet<Integer> SecondSet) {
  34.         HashSet<Integer> Set = new HashSet<>();
  35.         Set.addAll(FirstSet);
  36.         Set.addAll(SecondSet);
  37.         return Set;
  38.     }
  39.  
  40.    public static HashSet<Integer> EvenElements(HashSet<Integer> Set){
  41.         HashSet<Integer> EvenSet = new HashSet<>();
  42.         for (int i = 0; i < 255 ; i++ );{
  43.            Integer i = null;
  44.            if ( Set.contains(i)){
  45.                if(i % 2 == 0){
  46.                    EvenSet.add(i);
  47.                }
  48.             }
  49.        }
  50.        return EvenSet;
  51.    }
  52.  
  53.     public  static void SaveFile(HashSet<Integer> EvenSet) throws IOException {
  54.         Scanner in = new Scanner(System.in);
  55.         String outputFileName;
  56.         System.out.println("Введите имя файла в который хотите вывести данные:");
  57.         outputFileName = in.nextLine();
  58.         FileWriter out = new FileWriter(outputFileName);
  59.         out.write("\n  " + EvenSet);
  60.         out.close();
  61.     }
  62.  
  63.  
  64.     public static void main(String[] args) throws IOException {
  65.         HashSet<Integer> FirstSet = ReadFromFileFirst();
  66.         HashSet<Integer> SecondSet = ReadFromFileSecond();
  67.         HashSet<Integer> Set = UnionOfSets( FirstSet , SecondSet);
  68.         HashSet<Integer> EvenSet = EvenElements(Set);
  69.         SaveFile(EvenSet);
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement