|
| 1 | +package org.javacore.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 | + * Copyright [2015] [Jeff Lee] |
| 14 | + * |
| 15 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 16 | + * you may not use this file except in compliance with the License. |
| 17 | + * You may obtain a copy of the License at |
| 18 | + * |
| 19 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | + * |
| 21 | + * Unless required by applicable law or agreed to in writing, software |
| 22 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 23 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 24 | + * See the License for the specific language governing permissions and |
| 25 | + * limitations under the License. |
| 26 | + */ |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Jeff Lee |
| 30 | + * @since 2015-9-30 16:10:18 |
| 31 | + * 文本文件的操作案例 |
| 32 | + */ |
| 33 | +public class TextFile extends ArrayList<String> { |
| 34 | + |
| 35 | + private static final long serialVersionUID = -6710557138987846943L; |
| 36 | + |
| 37 | + // 读取文件内容,返回内容字符串 |
| 38 | + public static String read(String fileName) { |
| 39 | + StringBuilder sb = new StringBuilder(); |
| 40 | + try { |
| 41 | + // 创建缓存字符输入流 |
| 42 | + BufferedReader in = new BufferedReader(new FileReader(// 创建读取字符文件类 |
| 43 | + new File(fileName).getAbsolutePath()));// 文件绝对路径地址 |
| 44 | + try { |
| 45 | + String s; |
| 46 | + // 读取一个文本行 |
| 47 | + while ((s = in.readLine()) != null) { |
| 48 | + sb.append(s); |
| 49 | + sb.append("\n"); |
| 50 | + } |
| 51 | + } finally { |
| 52 | + in.close(); |
| 53 | + } |
| 54 | + } catch (IOException e) { |
| 55 | + throw new RuntimeException(e); |
| 56 | + } |
| 57 | + return sb.toString(); |
| 58 | + } |
| 59 | + |
| 60 | + // 将字符串写入一个文本文件 |
| 61 | + public static void write(String fileName,String text) { |
| 62 | + try { |
| 63 | + // 创建打印输出流 |
| 64 | + PrintWriter out = new PrintWriter( |
| 65 | + new File(fileName).getAbsolutePath()); |
| 66 | + try { |
| 67 | + // 写入字符流 |
| 68 | + out.write(text); |
| 69 | + } finally { |
| 70 | + out.close(); |
| 71 | + } |
| 72 | + } catch (IOException e) { |
| 73 | + throw new RuntimeException(e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + // 通过正则匹配,读取文件 |
| 78 | + public TextFile(String fileName,String splitter) { |
| 79 | + super(Arrays.asList(read(fileName).split(splitter))); |
| 80 | + // 移除一个空格位置 |
| 81 | + if (get(0).equals("")) remove(0); |
| 82 | + } |
| 83 | + |
| 84 | + public TextFile(String fileName) { |
| 85 | + this(fileName, "\n"); |
| 86 | + } |
| 87 | + |
| 88 | + // 写入一个文本文件 |
| 89 | + public void write(String fileName) { |
| 90 | + try { |
| 91 | + // 创建打印输出流 |
| 92 | + PrintWriter out = new PrintWriter( |
| 93 | + new File(fileName).getAbsolutePath()); |
| 94 | + try { |
| 95 | + for (String item : this) |
| 96 | + out.write(item); |
| 97 | + } finally { |
| 98 | + out.close(); |
| 99 | + } |
| 100 | + } catch (IOException e) { |
| 101 | + throw new RuntimeException(e); |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | + public static void main(String[] args) { |
| 107 | + // 读取文件 |
| 108 | + String file = read("src/org/javacore/io/TextFile.java"); |
| 109 | + // 写入到test.txt |
| 110 | + write("test.txt", file); |
| 111 | + |
| 112 | + TextFile text = new TextFile("test.txt"); |
| 113 | + text.write("test2.txt"); |
| 114 | + |
| 115 | + TreeSet<String> words = new TreeSet<>( |
| 116 | + new TextFile("src/org/javacore/io/TextFile.java","\\W+")); |
| 117 | + System.out.println(words.headSet("a")); |
| 118 | + |
| 119 | + } |
| 120 | +} |
0 commit comments