mc_zefirka

My public class IOUtils

Feb 15th, 2021 (edited)
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.08 KB | None | 0 0
  1. package com.itstep.mvn.app.io;
  2.  
  3. import java.io.Closeable;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.FileReader;
  8. import java.io.FileWriter;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.io.OutputStream;
  13. import java.io.Reader;
  14. import java.io.Writer;
  15. import java.util.HashMap;
  16. import java.util.Map;
  17. import java.util.TreeMap;
  18.  
  19. import com.itstep.mvn.app.util.DateTime;
  20.  
  21. public class IOUtils {
  22.    
  23.     public static String getFileData(String path) {
  24.         File file = new File(path);
  25.         if(!file.exists()) {
  26.             System.out.println("File does not exists : " + path);
  27.             return null;
  28.         }
  29.         StringBuilder sb = new StringBuilder();
  30.         InputStream in = null;
  31.         InputStreamReader reader = null;
  32.         try {
  33.             in = new FileInputStream(path);
  34.             reader = new InputStreamReader(in, "windows-1251");
  35.             int byteOfData;
  36.             while ((byteOfData = reader.read()) != -1) {
  37.                 char ch = (char) byteOfData;
  38.                 sb.append(ch);
  39.             };
  40.         } catch (IOException e) {
  41.             e.printStackTrace();
  42.         } finally {
  43.             release(reader, in);
  44.         }
  45.  
  46.         return sb.toString();
  47.        
  48.     }
  49.  
  50.     public static void printFile(String path) {
  51.         File file = new File(path);
  52.         if(!file.exists()) {
  53.             System.out.println("File does not exists : " + path);
  54.             return;
  55.         }
  56.         System.out.println("======== PATH: " + path + " =========");
  57.         InputStream in = null;
  58.         try {
  59.             in = new FileInputStream(path);
  60.             int byteOfData;
  61.             while ((byteOfData = in.read()) != -1) {
  62.                 char ch = (char) byteOfData;
  63.                 System.out.print(ch);
  64.             };
  65.             System.out.println("\n=================");
  66.         } catch (IOException e) {
  67.             e.printStackTrace();
  68.         } finally {
  69.             release(in);
  70.         }
  71.     }
  72.  
  73.     public static void writeFile(String path, String txt) {
  74.         writeFile(path, txt, false);
  75.     }
  76.    
  77.     public static void writeFile(String path, String txt, boolean append) {
  78.         File file = new File(path);
  79.         OutputStream out = null;
  80.         try {
  81.             if(!file.exists())
  82.                 file.createNewFile();
  83.             out = new FileOutputStream(file, append);
  84.             if(file.length() != 0)
  85.                 out.write(System.getProperty("line.separator").getBytes());
  86.             for( char ch : txt.toCharArray()) {
  87.                 int b = (int)ch;
  88.                 out.write(b);
  89.             }
  90.             System.out.println("======== Writing File: " + path + " is DONE =========");
  91.         } catch (IOException e) {
  92.             // TODO Auto-generated catch block
  93.             e.printStackTrace();
  94.         } finally {
  95.             release(out);
  96.         }
  97.     }
  98.    
  99.     public static void writeTextStat(Map<String, Integer> stat) {
  100.         File file = new File("files/stat/STAT_" + DateTime.getCurrentTimeString() + ".txt");
  101.        
  102.         Writer writer = null;
  103.         try {
  104.             file.createNewFile();
  105.             writer = new FileWriter(file);
  106.            
  107.             for (String word : stat.keySet()) {
  108.                 writer.write(word + " -> " + stat.get(word));
  109.                 writer.write("\n"); //  new line
  110.             }
  111.             System.out.println("Stats");
  112.            
  113.         } catch (IOException e) {
  114.             // TODO Auto-generated catch block
  115.             e.printStackTrace();
  116.         } finally {
  117.             release(writer);
  118.         }
  119.        
  120.     }
  121.  
  122.     public static Map<String, Integer> getTextStat(String path){
  123.         Map<String, Integer> statsMap = new HashMap<>();
  124.        
  125.         File file = new File(path);
  126.         if(!file.exists()) {
  127.             System.out.println("File path:" + path + " NOT exists!");
  128.         }
  129.         Reader reader = null;
  130.         try {
  131.             reader = new FileReader(file);
  132.             int i;
  133.             // while the end of the stream has been reached
  134.             String word = "";
  135.             while( (i=reader.read()) != -1){
  136.                 char ch = (char)i;
  137.                 if(Character.isAlphabetic(ch)) {
  138.                     word += ch;
  139.                 } else if(ch == '\n' || ch == '\r'
  140.                         || Character.isDigit(ch)){
  141.                     // new line
  142.                     continue;
  143.                 } else {
  144.                     // find word, adding to the map
  145.                     if(word.trim().length() == 0) {
  146.                         continue;
  147.                     }
  148.                     word = word.toLowerCase();
  149.                     if(statsMap.containsKey(word)) {
  150.                         int count = statsMap.get(word);
  151.                         statsMap.put(word, ++count);
  152.                     } else {
  153.                         statsMap.put(word, 1);
  154.                     }
  155.                     //clean var
  156.                     word = "";
  157.                    
  158.                 }
  159.                
  160.                
  161.             }
  162.            
  163.         } catch (IOException e) {
  164.             e.printStackTrace();
  165.         } finally {
  166.             release(reader);
  167.                
  168.         }
  169.         // result is sorted
  170.         return new TreeMap<>(statsMap);
  171.        
  172.     }
  173.    
  174.     private static void release(Closeable... res) {
  175.         for (Closeable closeable : res) {
  176.             if(res != null) {
  177.                 try {
  178.                     closeable.close();
  179.                 } catch (IOException e) {
  180.                     e.printStackTrace();
  181.                 }
  182.             }
  183.         }
  184.        
  185.     }
  186.    
  187.  
  188. }
  189.  
Add Comment
Please, Sign In to add comment