Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. import java.util.regex.Matcher;
  2. import java.util.regex.Pattern;
  3. import java.io.*;
  4. import java.io.File;
  5.  
  6. public class zad3 {
  7.     public static void main(String[] args) {
  8.         System.out.println("Zadanie 3:");
  9.         String text="";
  10.         try {
  11.             FileInputStream fis = new FileInputStream("Z:\\PPJ\\cw27\\serverLog.txt");
  12.             int b = 0;
  13.             while ((b = fis.read()) != -1) {
  14.                 //System.out.print((char) b);
  15.                 text=text+(char)b;
  16.             }
  17.         }
  18.         catch (Exception e) {
  19.             System.out.print(e);
  20.         }
  21.  
  22.         String text1 = text;
  23.         String text2=" ";
  24.         String regex1 = "([0-9]{1,3}\\.){3}[0-9]{1,3}";
  25.         Pattern p1 = Pattern.compile(regex1);
  26.         Matcher m1 = p1.matcher(text1);
  27.         System.out.println(m1.matches());
  28.         while(m1.find()){
  29.             text2=text2+m1.group()+"\n";
  30.         }
  31.         System.out.println(text2);
  32.  
  33.         String text3="";
  34.         String regex2 = "[0-9]{1,3}";
  35.         Pattern p2 = Pattern.compile(regex2);
  36.         Matcher m2 = p2.matcher(text2);
  37.         System.out.println(m2.matches());
  38.         while(m2.find()){
  39.             System.out.println(m2.group());
  40.             text3=text3+Integer.toHexString(Integer.valueOf(m2.group()))+".";
  41.         }
  42.         System.out.println(text3);
  43.  
  44.         String text4="";
  45.         String regex3 = "(\\w{1,2}\\.){3}\\w{1,2}";
  46.         Pattern p3 = Pattern.compile(regex3);
  47.         Matcher m3 = p3.matcher(text3);
  48.         System.out.println(m3.matches());
  49.         int i=1;
  50.         while(m3.find()){
  51.             System.out.println(m3.group());
  52.             text4=text4+i+"\t"+m3.group()+"\n";
  53.             i=i+1;
  54.         }
  55.         System.out.println(text4);
  56.  
  57.         File file = new File("Z:\\PPJ\\cw27\\log2.txt");
  58.         try (FileOutputStream fop = new FileOutputStream(file)) {
  59.             if (!file.exists()) {
  60.                 file.createNewFile();
  61.             }
  62.             byte[] byte1 = text4.getBytes();
  63.             fop.write(byte1);
  64.             fop.flush();
  65.         } catch (IOException e) {
  66.             e.printStackTrace();
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement