daily pastebin goal
70%
SHARE
TWEET

Untitled

a guest Mar 22nd, 2018 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ru.schepin.chapter5.textFile;
  2.  
  3. import java.io.*;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.TreeSet;
  7.  
  8. public class TextFile extends ArrayList<String> {
  9.     public static String read(String filename) throws IOException {
  10.         StringBuilder stringBuilder = new StringBuilder();
  11.  
  12.         try (BufferedReader bufferedReader =
  13.                      new BufferedReader(new FileReader(new File(filename).getAbsoluteFile()))) {
  14.             String s;
  15.             while ((s = bufferedReader.readLine()) != null) {
  16.                 stringBuilder.append(s).append("\n");
  17.             }
  18.         }
  19.         return stringBuilder.toString();
  20.     }
  21.  
  22.     public static void write(String fileName, String text) throws FileNotFoundException {
  23.         try (PrintWriter printWriter = new PrintWriter
  24.                 (new File(fileName).getAbsoluteFile())) {
  25.             printWriter.print(text);
  26.         }
  27.     }
  28.  
  29.     public TextFile(String fileName, String splitter) throws IOException {
  30.         super(Arrays.asList(read(fileName).split(splitter)));
  31.         if (get(0).equals("")) remove(0);
  32.  
  33.     }
  34.  
  35.     public TextFile(String fileName) throws IOException {
  36.         this(fileName, "\n");
  37.     }
  38.  
  39.     public void write(String fileName) throws FileNotFoundException {
  40.         try (PrintWriter pr = new PrintWriter(new File(fileName).getAbsoluteFile())) {
  41.             for (String item : this) {
  42.                 pr.println(item);
  43.             }
  44.         }
  45.     }
  46.  
  47.     public static void main(String[] args) throws IOException {
  48.         String text = read("C:\\Users\\Dmitry\\Desktop\\Задача по Maven.txt");
  49.         write("TextFile", text);
  50.  
  51.         TextFile file = new TextFile("TextFile");
  52.         file.write("TextFile1");
  53.  
  54.         TreeSet<String> words = new TreeSet<>(new TextFile("TextFile1", "\\W+"));
  55.         System.out.println(words.headSet("a"));
  56.  
  57.     }
  58.  
  59.  
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top