Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.itstep.mvn.app.io;
- import java.io.Closeable;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.Reader;
- import java.io.Writer;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.TreeMap;
- import com.itstep.mvn.app.util.DateTime;
- public class IOUtils {
- public static String getFileData(String path) {
- File file = new File(path);
- if(!file.exists()) {
- System.out.println("File does not exists : " + path);
- return null;
- }
- StringBuilder sb = new StringBuilder();
- InputStream in = null;
- InputStreamReader reader = null;
- try {
- in = new FileInputStream(path);
- reader = new InputStreamReader(in, "windows-1251");
- int byteOfData;
- while ((byteOfData = reader.read()) != -1) {
- char ch = (char) byteOfData;
- sb.append(ch);
- };
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- release(reader, in);
- }
- return sb.toString();
- }
- public static void printFile(String path) {
- File file = new File(path);
- if(!file.exists()) {
- System.out.println("File does not exists : " + path);
- return;
- }
- System.out.println("======== PATH: " + path + " =========");
- InputStream in = null;
- try {
- in = new FileInputStream(path);
- int byteOfData;
- while ((byteOfData = in.read()) != -1) {
- char ch = (char) byteOfData;
- System.out.print(ch);
- };
- System.out.println("\n=================");
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- release(in);
- }
- }
- public static void writeFile(String path, String txt) {
- writeFile(path, txt, false);
- }
- public static void writeFile(String path, String txt, boolean append) {
- File file = new File(path);
- OutputStream out = null;
- try {
- if(!file.exists())
- file.createNewFile();
- out = new FileOutputStream(file, append);
- if(file.length() != 0)
- out.write(System.getProperty("line.separator").getBytes());
- for( char ch : txt.toCharArray()) {
- int b = (int)ch;
- out.write(b);
- }
- System.out.println("======== Writing File: " + path + " is DONE =========");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- release(out);
- }
- }
- public static void writeTextStat(Map<String, Integer> stat) {
- File file = new File("files/stat/STAT_" + DateTime.getCurrentTimeString() + ".txt");
- Writer writer = null;
- try {
- file.createNewFile();
- writer = new FileWriter(file);
- for (String word : stat.keySet()) {
- writer.write(word + " -> " + stat.get(word));
- writer.write("\n"); // new line
- }
- System.out.println("Stats");
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- release(writer);
- }
- }
- public static Map<String, Integer> getTextStat(String path){
- Map<String, Integer> statsMap = new HashMap<>();
- File file = new File(path);
- if(!file.exists()) {
- System.out.println("File path:" + path + " NOT exists!");
- }
- Reader reader = null;
- try {
- reader = new FileReader(file);
- int i;
- // while the end of the stream has been reached
- String word = "";
- while( (i=reader.read()) != -1){
- char ch = (char)i;
- if(Character.isAlphabetic(ch)) {
- word += ch;
- } else if(ch == '\n' || ch == '\r'
- || Character.isDigit(ch)){
- // new line
- continue;
- } else {
- // find word, adding to the map
- if(word.trim().length() == 0) {
- continue;
- }
- word = word.toLowerCase();
- if(statsMap.containsKey(word)) {
- int count = statsMap.get(word);
- statsMap.put(word, ++count);
- } else {
- statsMap.put(word, 1);
- }
- //clean var
- word = "";
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- release(reader);
- }
- // result is sorted
- return new TreeMap<>(statsMap);
- }
- private static void release(Closeable... res) {
- for (Closeable closeable : res) {
- if(res != null) {
- try {
- closeable.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment