Created
July 26, 2016 04:03
-
-
Save AlbertHG/3fcb1802ba87776b39fbe67fd6eb4af8 to your computer and use it in GitHub Desktop.
文件读写的实用工具
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.TreeSet; | |
public class TextFile extends ArrayList<String> { | |
/** | |
* 静态方法,以String的形式读取一个文件 | |
* @param fileName 传入文件绝对路径 | |
* @return | |
*/ | |
public static String read(String fileName) { | |
StringBuilder sb = new StringBuilder(); | |
try { | |
BufferedReader in = new BufferedReader(new FileReader(new File(fileName).getAbsoluteFile())); | |
try { | |
String s; | |
//read()将每行添加到BufferedResader中 ,同时加入换行符,因为在读的过程中,换行符是被忽略的 | |
while ((s = in.readLine()) != null) { | |
sb.append(s); | |
sb.append("\n"); | |
} | |
} finally { | |
in.close(); | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
return sb.toString(); | |
} | |
/** | |
* 静态方法 从String中将文件写入File中 | |
* @param fileName 传入文件的绝对路径 | |
* @param text 将要写入文件的String串 | |
*/ | |
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 (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
public TextFile(String fileName, String splitter) { | |
//public ArrayList(Collection<? extends E> c) | |
//构造一个包含指定 collection 的元素的列表,这些元素是按照该 collection 的迭代器返回它们的顺序排列的 | |
super(Arrays.asList(read(fileName).split(splitter))); | |
if (get(0).equals("")) { | |
remove(0); | |
} | |
} | |
public TextFile(String fileName) { | |
this(fileName, "\n"); | |
} | |
/** | |
* 非静态方法 一行一行的将数据输出 | |
* @param fileName 要写入数据的绝对路径 | |
*/ | |
public void write(String fileName) { | |
try { | |
PrintWriter pw = new PrintWriter(new File(fileName).getAbsoluteFile()); | |
try { | |
for (String item : this) {//从this里取出数据,这里的this应该是继承ArrayList之后,在读取的时候将数据存储在父类的容器里 | |
pw.println(item);//一行一行的将数据写入缓冲区 | |
} | |
} finally { | |
pw.close();//关闭流 | |
} | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
//测试方法 | |
public static void main(String[] args) { | |
String file = read("H:/eclipse4_5_workspace/ThinkingOfJava/src/TextFile.java"); | |
write("H:/Dropbox/Desktop/text.txt", file); | |
TextFile.write("H:/Dropbox/Desktop/text2.txt", file); | |
TextFile tf = new TextFile("H:/eclipse4_5_workspace/ThinkingOfJava/src/TextFile.java"); | |
tf.write("H:/Dropbox/Desktop/text3.txt"); | |
TreeSet<String> words = new TreeSet<String>(new TextFile("H:/eclipse4_5_workspace/ThinkingOfJava/src/TextFile.java", "\\W+")); | |
System.out.println(words.headSet("a")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
这个类用来简化对文件的读写操作,它包含的static方法可以像简单字符串那样读写文件,并且可以通过构造器创建一个FileText对象,用ArrayList保存文件的入若干行(如此,在进行文件内容操作的时候,就可以使用ArrayList的所有功能了)