Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.62 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5.  
  6. class lab3 extends Thread {
  7.  
  8.     public boolean isTrue (StringBuilder[] str){
  9.         for (StringBuilder stringBuilder : str) {
  10.             if (stringBuilder.length() < 0)
  11.                 return false;
  12.         }
  13.         return true;
  14.     }
  15.  
  16.     @Override
  17.     public void run() {
  18.         FileWriter fw = null;
  19.         try {
  20.             fw = new FileWriter("result.txt");
  21.         } catch (IOException e) {
  22.             e.printStackTrace();
  23.         }
  24.         assert fw != null;
  25.         TreeSet<String> set = new TreeSet<>();
  26.  
  27.         BufferedWriter bufferWriter = new BufferedWriter(fw);
  28.         WriteFile[] obj_arr = new WriteFile[10];
  29.         StringBuffer[] str = new StringBuffer[10];
  30.         for(int i = 0; i<10; i++){
  31.             str[i] =  new StringBuffer();
  32.             obj_arr[i] = new WriteFile(bufferWriter, str[i]);
  33.             obj_arr[i].start();
  34.         }
  35.         int before = 3, after = 3;
  36.         String searchLine = "123";
  37.         ArrayList<String> lines = new ArrayList<>();
  38.         Scanner filein = null;
  39.         try {
  40.             filein = new Scanner(new File("file.txt"));
  41.         } catch (FileNotFoundException e) {
  42.             e.printStackTrace();
  43.         }
  44.         assert filein != null;
  45.         FileReader input = null;
  46.         try {
  47.             input = new FileReader("file.txt");
  48.         } catch (FileNotFoundException e) {
  49.             e.printStackTrace();
  50.         }
  51.         assert input != null;
  52.         LineNumberReader count = new LineNumberReader(input);
  53.         long count_lines = 0;
  54.         while (true)
  55.         {
  56.             try {
  57.                 if (!(count.skip(Long.MAX_VALUE) > 0)) break;
  58.             } catch (IOException e) {
  59.                 e.printStackTrace();
  60.             }
  61.         }
  62.         count_lines = count.getLineNumber() + 1;
  63.         int output_strings =0, current_count_lines = 0, currentThreadIndex = 0;
  64.         long start = System.currentTimeMillis();
  65.         ArrayDeque<Integer> Arr = new ArrayDeque<>();
  66.         while (filein.hasNextLine()){
  67.             current_count_lines++;
  68.             lines.add(filein.nextLine());
  69.             if(lines.size() > before + after + 1){
  70.                 lines.remove(0);
  71.             }
  72.             if (lines.size() >= after + 1){
  73.  
  74.                 if(lines.get(lines.size() - after - 1).contains(searchLine)){
  75.                     Arr.add(output_strings);
  76.                     StringBuilder tmp = new StringBuilder();
  77.                     for (String line:
  78.                             lines) {
  79.                         output_strings++;
  80.                         tmp.append(output_strings);
  81.                         tmp.append(" ");
  82.                         tmp.append(line);
  83.                         tmp.append("\n");
  84.                     }
  85.                     while (str[0].length() > 0 && str[1].length() > 0 && str[2].length() > 0 && str[3].length() > 0 &&
  86.                             str[4].length() > 0 && str[5].length() > 0 && str[6].length() > 0 && str[7].length() > 0 &&
  87.                             str[8].length() > 0 && str[9].length() > 0){
  88.  
  89.                         try {
  90.                             sleep(1);
  91.                         } catch (InterruptedException e) {
  92.                             e.printStackTrace();
  93.                         }
  94.                     }
  95.  
  96.                     if(str[0].length() == 0) {
  97.                         str[0].append(tmp);
  98.  
  99. //                        Arr.add(output_strings);
  100.                     }else if(str[1].length() == 0) {
  101.                         str[1].append(tmp);
  102.                         Arr.add(output_strings);
  103.                     }else if(str[2].length() == 0) {
  104.                         str[2].append(tmp);
  105.                     }else if(str[3].length() == 0) {
  106.                         str[3].append(tmp);
  107.                     }else if(str[4].length() == 0) {
  108.                         str[4].append(tmp);
  109.                     }else if(str[5].length() == 0) {
  110.                         str[5].append(tmp);
  111.                     }else if(str[6].length() == 0) {
  112.                         str[6].append(tmp);
  113.                     }else if(str[7].length() == 0) {
  114.                         str[7].append(tmp);
  115.                     }else if(str[8].length() == 0) {
  116.                         str[8].append(tmp);
  117.                     }else{
  118.                         str[9].append(tmp);
  119.                     }
  120. //                    lines.remove(0);
  121.                 }
  122.             }
  123.             long time = System.currentTimeMillis() - start;
  124.             long cur = (count_lines - current_count_lines)*(time/1000)/current_count_lines;
  125.             System.out.println("Найдено совпадений "+output_strings);
  126.             System.out.println("Осталось работать "+cur+" сек");
  127.  
  128.         }
  129.         lines.remove(0);
  130.         for(int i = before + 1; i < lines.size(); i++) {
  131.             if (lines.get(i).contains(searchLine)) {
  132.                 Arr.add(output_strings);
  133.                 StringBuilder tmp = new StringBuilder();
  134.                 for (String line :
  135.                         lines) {
  136.                     output_strings++;
  137.                     tmp.append(output_strings);
  138.                     tmp.append(" ");
  139.                     tmp.append(line);
  140.                     tmp.append("\n");
  141.                 }
  142.                 while (str[0].length() > 0 && str[1].length() > 0 && str[2].length() > 0 && str[3].length() > 0 &&
  143.                         str[4].length() > 0 && str[5].length() > 0 && str[6].length() > 0 && str[7].length() > 0 &&
  144.                         str[8].length() > 0 && str[9].length() > 0) {
  145.  
  146.                     try {
  147.                         sleep(1);
  148.                     } catch (InterruptedException e) {
  149.                         e.printStackTrace();
  150.                     }
  151.                 }
  152.  
  153.                 if (str[0].length() == 0) {
  154.                     str[0].append(tmp);
  155.  
  156. //                        Arr.add(output_strings);
  157.                 } else if (str[1].length() == 0) {
  158.                     str[1].append(tmp);
  159.                     Arr.add(output_strings);
  160.                 } else if (str[2].length() == 0) {
  161.                     str[2].append(tmp);
  162.                 } else if (str[3].length() == 0) {
  163.                     str[3].append(tmp);
  164.                 } else if (str[4].length() == 0) {
  165.                     str[4].append(tmp);
  166.                 } else if (str[5].length() == 0) {
  167.                     str[5].append(tmp);
  168.                 } else if (str[6].length() == 0) {
  169.                     str[6].append(tmp);
  170.                 } else if (str[7].length() == 0) {
  171.                     str[7].append(tmp);
  172.                 } else if (str[8].length() == 0) {
  173.                     str[8].append(tmp);
  174.                 } else {
  175.                     str[9].append(tmp);
  176.                 }
  177.                 lines.remove(0);
  178.             }
  179.         }
  180.  
  181.         while (str[0].length() > 0 || str[1].length() > 0 || str[2].length() > 0 ||str[3].length() > 0 ||
  182.                 str[4].length() > 0 || str[5].length() > 0 || str[6].length() > 0 || str[7].length() > 0 ||
  183.                 str[8].length() > 0 || str[9].length() > 0) {
  184.             try {
  185.                 sleep(1);
  186.             } catch (InterruptedException e) {
  187.                 e.printStackTrace();
  188.             }
  189.         }
  190.         try {
  191.             bufferWriter.close();
  192.             fw.close();
  193.             filein.close();
  194.         } catch (IOException e) {
  195.             e.printStackTrace();
  196.         }
  197.         for(int i = 0; i<10; i++){
  198.             obj_arr[i].interrupt();
  199.         }
  200. //        ArrayList<StringBuffer> str2;
  201.         this.interrupt();
  202.     }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement