TextFile 作为自写的方法,继承自List<String>。像统计文本中出现的哪些单词,不重复等等,适合用Set集合完成统计。

 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(IOException e){
throw new RuntimeException(e);
}
return sb.toString();
}
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(IOException e){
throw new RuntimeException(e);
} }
public static void write(String filename,String text){
// 其中filename指明要写入的文件名,text指明写入的字符串内容
try{
FileWriter fwriter=new FileWriter(new File
(filename).getAbsoluteFile());
BufferedWriter out=new BufferedWriter(fwriter);
String []tx=text.split("\n");
try{
for(int i=0;i<tx.length;i++)
{
out.write(tx[i]);
out.newLine();
}
}finally{
out.flush();
out.close();
}
}catch(IOException e){
throw new RuntimeException(e); } }
}
    public static void main(String[] args) {
Set<String> words = new TreeSet<String>(
new TextFile("StatckTest.java","\\W+"));
System.out.print(words);
System.out.println(words.size()); Set<String> words2 = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
words2.addAll(words);
System.out.print(words2);
System.err.println(words2.size()); //这里有什么不同的? }

TextFile 类的创写的更多相关文章

  1. 基类的析构函数写成virtual虚析构函数

    虚函数作用:动态绑定,实现多态效果. 场景问题: 派生类中有资源需要回收,而在编程中采用多态,由基类的指针指向派生类,则在释放的时候,如果基类的析构函数不是virtual,则派生类的析构函数得不到释放 ...

  2. C++模板类代码只能写在头文件?

      这个问题,实际上我几年前就遇到了.最近写个模板类玩的时候,再次遇到.   当我非常仔细的将定义和实现分开,在头文件中保留了最少的依赖后,一切就绪.cpp单独编过.但是当使用的时候,就会报告所有的函 ...

  3. 需要序列化的类中没有写serialVersionUID的解决办法

    由于没赋值serialVersionUID 只是警告,不是错误,造成先前没留意设定serialVersionUID,网络两端上线运行一段时间也感觉正常.如果再增减修改field,没赋值好serialV ...

  4. Java——IO类,字节流写数据

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  5. QT in VS 多语言实现(中英文切换,每个步骤都有截图,只有UTF8才能让Qt语言家正确读取。先qApp->removeTranslator,然后installTranslator,每个类都要写上槽函数RetranslateUI)

    最近项目需要软件具有中英文双语切换功能,而QT又自带此功能,现将实现方式记录下来. 说到中英文切换,少不了要了解QT的内部编码方式.在此就不详述QT编码方式了,具体可参考 彻底弄懂Qt的编码.只需要记 ...

  6. 20162326 齐力锋 2017-2018学期 Bag类的补写博客

    要求: 代码运行在命令行中,路径要体现学号信息,IDEA中,伪代码要体现个人学号信息 参见Bag的UML图,用Java继承BagInterface实现泛型类Bag,并对方法进行单元测试(JUnit), ...

  7. [转]C# 将类的内容写成JSON格式的字符串

    将类的内容写入到JSON格式的字符串中 本例中建立了Person类,赋值后将类中内容写入到字符串中 运行本代码需要添加引用动态库Newtonsoft.Json 程序代码: using System; ...

  8. 收集C#常用类:自己写的一个DBHelper类

    随着学的东西越来越多,一点点的完善吧! using System; using System.Collections.Generic; using System.Linq; using System. ...

  9. Android 线程池系列教程(2)Thread,Runnable是基类及如何写Run方法

    Specifying the Code to Run on a Thread 上一课   下一课 1.This lesson teaches you to Define a Class that Im ...

随机推荐

  1. Hbase客户端API基础小结笔记(未完)

    客户端API:基础 HBase的主要客户端接口是由org.apache.hadoop.hbase.client包中的HTable类提供的,通过这个类,用户可以完成向HBase存储和检索数据,以及删除无 ...

  2. MRD

    搜索 复制

  3. DestroyWindow函数注意事项

    最近遇到这样一个问题:将一个窗口句柄以参数的形式传递给一个线程,在线程中使用完之后要将窗口销毁,调用DestroyWindow销毁窗口是返回false,GetLastError的结果为5:拒绝访问,而 ...

  4. xcode 打静态库.a文件

    原文地址:http://blog.csdn.net/pjk1129/article/details/7255163 核心命令:lipo -info 地址.查看支持的类型,如armv7 lipo -cr ...

  5. 17. Word Break && Word Break II

    Word Break Given a string s and a dictionary of words dict, determine if s can be segmented into a s ...

  6. attr与prop

    Jquery获取checkbox属性checked为undefined (-- ::)转载▼ 标签: js jquery checkbox checked undefined 分类: JQuery 使 ...

  7. WinCE6.0多国语言软键盘

    N久以前写过一篇<WinCE下自定义的大软键盘>,这个自定义软键盘就是为RM905a+项目来做的.RM905a+的系统分辨率是640*480,WinCE原生键盘小的太小,大的又太大.所以就 ...

  8. 9. Linux远程登录

    1. 检查网络是否通畅 C:\Users\cfm>ping 192.168.232.131 正在 Ping 192.168.232.131 具有 32 字节的数据:来自 192.168.232. ...

  9. POS管理系统之设备出库

    JSP: <html>  <head>    <title>登录</title>   <style type="text/css&quo ...

  10. c# 支付宝查单补单

    谁谁谁手机那边支付宝支付成功扣钱了,游戏里面却没有充钱,唯有写个补单程序. 首先要查单,查单接口(交易信息查询接口:https://mapi.alipay.com/gateway.do),服务名称:s ...