-
Notifications
You must be signed in to change notification settings - Fork 0
/
testStaic.txt
1 lines (1 loc) · 1.96 KB
/
testStaic.txt
1
package org.rui.io.util;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Arrays;import java.util.TreeSet;public class TextFile extends ArrayList<String>{ public static String read(String fileName) { StringBuilder sb=new StringBuilder(); try { BufferedReader in=new BufferedReader( new FileReader( new File(fileName).getAbsoluteFile()) ); try { String s; while((s=in.readLine())!=null) sb.append(s); sb.append("\n"); } finally { in.close(); } } catch (Exception e) { throw new RuntimeException(e); } return sb.toString(); } // write a single public static void write(String fileName,String text) { try { PrintWriter out=new PrintWriter( new File(fileName).getAbsoluteFile() ); try { out.print(text); }finally { out.close(); } } catch (Exception e) { throw new RuntimeException(e); } } // read a file public TextFile(String fileName,String splitter) { super(Arrays.asList(read(fileName).split(splitter))); if(get(0).equals("")) remove(0); } public TextFile(String fileName) { this(fileName,"\n"); } public void write(String fileName) { try { PrintWriter out=new PrintWriter( new File(fileName).getAbsoluteFile() ); try { for(String item:this) out.println(item); } finally { out.close(); } } catch (Exception e) { throw new RuntimeException(e); } } /////simple test public static void main(String[] args) { //路径根据自已的改 String path="D:\\Users\\liangrui\\workspace\\thinking\\src\\org\\rui\\io\\util/"; //写入文件 String file =read(path+"TextFile.java"); write("testStaic.txt",file); //对象 写 TextFile tf=new TextFile(path+"text.txt"); tf.write(path+"test2.txt"); TreeSet<String> words=new TreeSet<String>( new TextFile("TextFile.java","\\w+") ); //headSet 返回此 set 的部分视图,其元素严格小于 toElement。 System.out.println(words.headSet("a")); }}