Advertisement
Guest User

java.io

a guest
Jan 23rd, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.ServerSocket;
  3. import java.net.Socket;
  4. import java.util.Scanner;
  5. import java.util.concurrent.Semaphore;
  6.  
  7. public class Main {
  8.  
  9. static Semaphore semaphore = new Semaphore(1);
  10. static BufferedWriter bw;
  11.  
  12. public static void main(String[] args) throws IOException {
  13. bw = new BufferedWriter(new FileWriter(new File("finki.txt")));
  14. ServerHolder serverT = new ServerHolder();
  15. serverT.start();
  16.  
  17. ClientHolder client1 = new ClientHolder("fold1");
  18. client1.start();
  19. ClientHolder client2 = new ClientHolder("fold2");
  20. client2.start();
  21. }
  22.  
  23. static class ServerHolder extends Thread {
  24. ServerSocket serverSocket;
  25.  
  26. public ServerHolder() throws IOException {
  27. serverSocket = new ServerSocket(3398);
  28. }
  29.  
  30. @Override
  31. public void run() {
  32. Socket client;
  33. while (true) {
  34. try {
  35. client = serverSocket.accept();
  36. ServerWorker sw = new ServerWorker(client);
  37. sw.start();
  38. } catch (IOException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. }
  43. }
  44.  
  45. static class ClientHolder extends Thread {
  46. private File fol;
  47. BufferedWriter writer;
  48.  
  49. ClientHolder(String ftoscn) throws IOException {
  50. this.fol = new File(ftoscn);
  51. }
  52.  
  53. @Override
  54. public void run() {
  55. try {
  56. Integer numFINKI = rek(fol);
  57. writer.close();
  58. Socket s = new Socket("localhost", 3398);
  59. DataOutputStream dos = new DataOutputStream(s.getOutputStream());
  60. dos.writeInt(numFINKI);
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65.  
  66. public Integer rek(File fol) throws IOException {
  67. Integer numFINKI = 0;
  68. for (File f : fol.listFiles()) {
  69. if (f.isFile() && (f.getName().endsWith(".txt") && f.getName().endsWith(".out"))
  70. || (f.length() < 100 * 1024 && f.length() > 1024)) {
  71. Scanner input = new Scanner(f);
  72. while (input.hasNext()) {
  73. String line = input.nextLine();
  74. if (line.contains("FINKI")) {
  75. numFINKI++;
  76. }
  77. }
  78. } else if (f.isDirectory()) {
  79. rek(f);
  80. }
  81. }
  82.  
  83. return numFINKI;
  84. }
  85. }
  86.  
  87. static class ServerWorker extends Thread {
  88. private final DataInputStream dis;
  89. Socket soc;
  90. public ServerWorker(Socket c) throws IOException {
  91. soc = c;
  92. dis = new DataInputStream(c.getInputStream());
  93. }
  94.  
  95. @Override
  96. public void run() {
  97. try {
  98. Integer numsStringFinki = dis.readInt();
  99. semaphore.acquire();
  100. bw.write(soc.getLocalAddress().toString() + " " + soc.getLocalPort() + " "
  101. + numsStringFinki + "\n");
  102. bw.flush();
  103. semaphore.release();
  104. } catch (IOException | InterruptedException e) {
  105. e.printStackTrace();
  106. }
  107.  
  108. }
  109.  
  110. }
  111.  
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement