|
| 1 | +package com.brianway.learning.java.io; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.File; |
| 5 | +import java.io.FileReader; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.PrintWriter; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Arrays; |
| 10 | +import java.util.TreeSet; |
| 11 | + |
| 12 | +/** |
| 13 | + * Static functions for reading and writing text files as |
| 14 | + * a single string, and treating a file as an ArrayList. |
| 15 | + */ |
| 16 | +public class TextFile extends ArrayList<String> { |
| 17 | + // Read a file as a single string: |
| 18 | + public static String read(String fileName) { |
| 19 | + StringBuilder sb = new StringBuilder(); |
| 20 | + try { |
| 21 | + BufferedReader in = new BufferedReader( |
| 22 | + new FileReader( |
| 23 | + new File(fileName).getAbsoluteFile())); |
| 24 | + try { |
| 25 | + String s; |
| 26 | + while ((s = in.readLine()) != null) { |
| 27 | + sb.append(s); |
| 28 | + sb.append("\n"); |
| 29 | + } |
| 30 | + } finally { |
| 31 | + try { |
| 32 | + in.close(); |
| 33 | + } catch (IOException e) { |
| 34 | + e.printStackTrace(); |
| 35 | + } |
| 36 | + |
| 37 | + } |
| 38 | + } catch (IOException e) { |
| 39 | + throw new RuntimeException(e); |
| 40 | + } |
| 41 | + return sb.toString(); |
| 42 | + } |
| 43 | + |
| 44 | + // Write a single file in one method call: |
| 45 | + public static void write(String fileName, String text) { |
| 46 | + try { |
| 47 | + PrintWriter out = new PrintWriter( |
| 48 | + new File(fileName).getAbsoluteFile()); |
| 49 | + try { |
| 50 | + out.print(text); |
| 51 | + } finally { |
| 52 | + out.close(); |
| 53 | + } |
| 54 | + } catch (IOException e) { |
| 55 | + throw new RuntimeException(e); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + // Read a file, split by any regular expression: |
| 60 | + public TextFile(String fileName, String splitter) { |
| 61 | + super(Arrays.asList(read(fileName).split(splitter))); |
| 62 | + // Regular expression split() often leaves an empty |
| 63 | + // String at the first position: |
| 64 | + if (get(0).equals("")) remove(0); |
| 65 | + } |
| 66 | + |
| 67 | + // Normally read by lines: |
| 68 | + public TextFile(String fileName) { |
| 69 | + this(fileName, "\n"); |
| 70 | + } |
| 71 | + |
| 72 | + public void write(String fileName) { |
| 73 | + try { |
| 74 | + PrintWriter out = new PrintWriter( |
| 75 | + new File(fileName).getAbsoluteFile()); |
| 76 | + try { |
| 77 | + for (String item : this) |
| 78 | + out.println(item); |
| 79 | + } finally { |
| 80 | + out.close(); |
| 81 | + } |
| 82 | + } catch (IOException e) { |
| 83 | + throw new RuntimeException(e); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Simple test: |
| 88 | + public static void main(String[] args) { |
| 89 | + String parent = BufferedInputFile.class.getResource("/").getPath(); |
| 90 | + String inFileName = parent + "/infile.txt"; |
| 91 | + |
| 92 | + String file = read(inFileName); |
| 93 | + write(parent + "/text.txt", file); |
| 94 | + TextFile text = new TextFile(parent + "/text.txt"); |
| 95 | + text.write(parent + "test2.txt"); |
| 96 | + // Break into unique sorted list of words: |
| 97 | + TreeSet<String> words = new TreeSet<String>( |
| 98 | + new TextFile(inFileName, "\\W+")); |
| 99 | + // Display the capitalized words: |
| 100 | + System.out.println(words.headSet("a")); |
| 101 | + |
| 102 | + } |
| 103 | +} |
| 104 | + |
0 commit comments