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. smarty3与2的差异导致的小问题

    又是一天看视频~ 今天在PHP100上学习smartY教程,视频中讲到了在模板文件中直接写带有“{}”JAVASCRIPT脚本会报错,我照视频上的代码写了下来,如下: <script> f ...

  2. ADF_Desktop Integration系列1_ADF桌面集成入门之设定Development Environment

    2013-05-01 Created By BaoXinjian

  3. Linux命令(21)查看文件的行数

    在 linux 系统中没有在 windows 系统中那么方便的点点鼠标就可以操作文件了,对文件的各种操作都必须使用各种命令来完成.比如有时候我们需要在不查看文件内容的情况下需要知道该文件有多少行.这个 ...

  4. Bugtags 介绍视频 - App 测试 · 从未如此简单

    Bugtags 是什么? Bugtags 是移动时代首选 Bug 管理系统,针对不同的使用场景,Bugtags 具有以下强大特性: 移动应用 Bug 管理 Bugtags 可以直接在应用中所见即所得提 ...

  5. 2.Median of Two Sorted Arrays (两个排序数组的中位数)

    要求:Median of Two Sorted Arrays (求两个排序数组的中位数) 分析:1. 两个数组含有的数字总数为偶数或奇数两种情况.2. 有数组可能为空. 解决方法: 1.排序法 时间复 ...

  6. JS适配问题。

    动画requestAnimFrame + cancelAnimationFrame window.requestAnimFrame = (function(){ return window.reque ...

  7. 通过微信分享链接,后面会被加上from=singlemessage&isappinstalled=1可能导致网页打不开

    微信分享会根据分享的不同,为原始链接拼接如下参数: 朋友圈   from=timeline&isappinstalled=0 微信群   from=groupmessage&isapp ...

  8. web项目启动报错Unknown character set: 'utf8mb4' in mysql

    网上一查,有的说是mysql驱动的问题,有的说创建数据库的时候指定utf8编码,换了各种mysql版本,最后换了5.1.6版本的mysql驱动后成功启动!问题解决!OMG

  9. WebForm---增删改(内置对象)

    一.添加 前台代码: <body> <form id="form1" runat="server"> <h1>用户添加< ...

  10. jafka的zk数据

    查看topics: ls /brokers/topics [mytopic] 查看topic所在的broker,下面例子,mytopic在broker 0 中管理. ls /brokers/topic ...