Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. package cwiczymy;
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7.  
  8. public class Cwiczymy {
  9.  
  10.     public static void main(String[] args) throws IOException
  11.     {
  12.         String sciezkaWej = "dane.txt";
  13.         String sciezkaWyj = "dane - kopia.txt ";
  14.        
  15.         Stworz(sciezkaWej);
  16.         Przepisz(sciezkaWej, sciezkaWyj);
  17.         Licz(sciezkaWej);
  18.     }
  19.    
  20.     public static void Stworz(String sciezka)
  21.     {
  22.         try
  23.         {
  24.             BufferedWriter bW = new BufferedWriter(new FileWriter(sciezka));
  25.             bW.write("Ala ma kota");
  26.             bW.newLine();
  27.             bW.write("kot ma Ale");
  28.             bW.newLine();
  29.             bW.write("Virtus.pro gra jak gówno");
  30.             bW.newLine();
  31.             bW.write("To jest taniec to jest życie");
  32.             bW.newLine();
  33.             bW.write("Lubie placki");
  34.            
  35.             bW.close();
  36.         }
  37.         catch (IOException ex)
  38.         {
  39.             System.out.println(ex.getMessage());
  40.         }
  41.     }
  42.    
  43.     public static void Przepisz(String sciezkaWejscia,String sciezkaWyjscia)
  44.     {
  45.         try
  46.         {
  47.             BufferedReader bR = new BufferedReader(new FileReader(sciezkaWejscia));
  48.             BufferedWriter bW = new BufferedWriter(new FileWriter(sciezkaWyjscia));
  49.             String linijka = "";
  50.            
  51.             while ((linijka = bR.readLine()) != null)
  52.             {
  53.                 bW.write(linijka);
  54.                 bW.newLine();
  55.                 bR.readLine();
  56.             }
  57.            
  58.             bW.close();
  59.             bR.close();
  60.            
  61.         }
  62.         catch (FileNotFoundException ex)
  63.         {
  64.              System.out.println(ex.getMessage());
  65.         }
  66.         catch (IOException ex)
  67.         {
  68.             System.out.println(ex.getMessage());
  69.         }
  70.     }
  71.    
  72.     public static int[] Licz(String sciezka) throws IOException
  73.     {
  74.         Scanner plikWe = null;
  75.         int iloscWyrazow = 0;
  76.         int iloscCharow = 0;
  77.         int iloscBialychZnakow = 0;
  78.         int [] tabZwrotna = new int [3];
  79.        
  80.         try
  81.         {
  82.             plikWe = new Scanner(new BufferedReader(new FileReader(sciezka)));
  83.             BufferedWriter dopisz = new BufferedWriter(new FileWriter(sciezka, true));
  84.             FileReader plikWe_1 = new FileReader(sciezka);
  85.            
  86.             // liczenie wyrazów w tekście
  87.             while (plikWe.hasNext())
  88.             {
  89.                 plikWe.next();
  90.                 iloscWyrazow++;
  91.             }
  92.            
  93.             // liczenie charów w tekscie
  94.             int c = 0;
  95.             while ((c = plikWe_1.read()) != -1)
  96.             {
  97.                 if (Character.isWhitespace((char)(c)) != true)
  98.                 {
  99.                     iloscCharow++;
  100.                 }
  101.                 else if (Character.isWhitespace((char)(c)))
  102.                 {
  103.                     iloscBialychZnakow++;
  104.                 }
  105.             }
  106.            
  107.             dopisz.newLine();
  108.             dopisz.newLine();
  109.             dopisz.append("Ilość wyrazów to: " + iloscWyrazow);
  110.             dopisz.newLine();
  111.             dopisz.append("Ilość charów to: " + iloscCharow);
  112.             dopisz.newLine();
  113.             dopisz.append("Ilość bialych znakow: " + iloscBialychZnakow);
  114.            
  115.             dopisz.close();
  116.             plikWe.close();
  117.         }
  118.         catch (FileNotFoundException ex)
  119.         {
  120.              System.out.println(ex.getMessage());
  121.         }
  122.        
  123.         tabZwrotna[0] = iloscWyrazow;
  124.         tabZwrotna[1] = iloscCharow;
  125.         tabZwrotna[2] = iloscBialychZnakow;
  126.        
  127.         return tabZwrotna;
  128.     }
  129.    
  130.     public static void szukaj(String sciezkaWej, String sciezkaWyj, String wyraz)
  131.     {
  132.        
  133.     }
  134.    
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement