adamnoeva

InnaShobik3

Mar 20th, 2022
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. package by.shobik.lw3;
  2.  
  3. import java.io.*;
  4. import java.nio.charset.StandardCharsets;
  5. import java.util.Base64;
  6.  
  7. public class Main {
  8.  
  9.     /* Работа со строками. Чтение исходной строки осуществлять из текстового файла,
  10.     для результирующей строки применить стандартный алгоритм шифрования и записать
  11.     в файл. При выполнении следующих заданий для вывода результатов создавать
  12.     новую директорию и файл средствами класса File.
  13.     30. Вывести в заданном тексте все слова, расположив их в алфавитном порядке.
  14.      */
  15.  
  16.     public final static String path = "input.txt";
  17.  
  18.     public static String readFile(String path) throws IOException {
  19.         BufferedReader br = new BufferedReader(new FileReader(path));
  20.         StringBuilder sb = new StringBuilder();
  21.  
  22.         String line = br.readLine();
  23.         while (line != null) {
  24.             sb.append(line)
  25.               .append(" ");
  26.             line = br.readLine();
  27.         }
  28.         br.close();
  29.         return sb.toString();
  30.     }
  31.  
  32.     public static String encode(String text) {
  33.         return Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8));
  34.     }
  35.  
  36.     public static void writeToFile(String path, String text) throws IOException {
  37.         BufferedWriter bw = new BufferedWriter(new FileWriter(path));
  38.         bw.write(text);
  39.         bw.close();
  40.     }
  41.  
  42.     public static void swap (String[] array, int i, int j) {
  43.         String temp = array[i];
  44.         array[i] = array[j];
  45.         array[j] = temp;
  46.     }
  47.  
  48.     public static String sortWords(String text) {
  49.  
  50.         String[] words = text.split("[ .,:;]");
  51.         for (int i = 0; i < words.length; i++) {
  52.             for (int j = i + 1; j < words.length; j++) {
  53.                 if (words[i].compareTo(words[j]) > 0) {
  54.                     swap(words, i, j);
  55.                 }
  56.             }
  57.         }
  58.         return String.join(" ", words).strip();
  59.  
  60.     }
  61.  
  62.     public static void main(String[] args) {
  63.  
  64.         String fileContent;
  65.         try { fileContent = readFile(path); }
  66.         catch (IOException e) {
  67.             System.out.println("Error while reading a file.");
  68.             return;
  69.         }
  70.  
  71.         System.out.println("Original content:\n" +
  72.                 fileContent);
  73.  
  74.         fileContent = sortWords(fileContent);
  75.         System.out.println(
  76.                 "Sorted by alphabet:\n" +
  77.                 sortWords(fileContent));
  78.  
  79.  
  80.         File dir = new File("result");
  81.         if (dir.mkdir()) {
  82.             System.out.println("Directory created successfully.");
  83.         } else {
  84.             System.out.println("Directory already exists.");
  85.         }
  86.  
  87.         String encoded = encode(fileContent);
  88.         try { writeToFile("result/output.txt", encoded); }
  89.         catch (IOException e) {
  90.             System.out.println("Failed to write to file.");
  91.         }
  92.  
  93.     }
  94. }
  95.  
Add Comment
Please, Sign In to add comment