TextFile 类的创写

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

 1 class TextFile extends ArrayList<String>{
 2         public static String read(String filename){
 3             StringBuilder sb=new StringBuilder();
 4             try{
 5                 BufferedReader in=new BufferedReader(new FileReader(
 6                         new File(filename).getAbsoluteFile()));
 7                     try{
 8                         String s;
 9                         while((s=in.readLine())!=null){
10                             sb.append(s);
11                             sb.append("\n");
12                             }
13                         }finally{
14                 in.close();
15                 }
16             }catch(IOException e){
17                 throw new RuntimeException(e);
18             }
19             return sb.toString();
20     }
21     public TextFile(String filename,String splitter){
22         super(Arrays.asList(read(filename).split(splitter)));
23         if(get(0).equals("")) remove(0);
24     }
25     public TextFile(String filename){
26         this(filename,"\n");
27     }
28     public void write(String filename){
29         try{
30             PrintWriter out=new PrintWriter(new File
31                     (filename).getAbsoluteFile());
32             try{
33                 for(String item : this) out.println(item);
34             }finally{
35                 out.close();
36                 }
37             }catch(IOException e){
38                 throw new RuntimeException(e);
39                 }
40             
41     }
42     public static void write(String filename,String text){
43     // 其中filename指明要写入的文件名,text指明写入的字符串内容
44         try{
45             FileWriter fwriter=new FileWriter(new File
46                     (filename).getAbsoluteFile());
47             BufferedWriter out=new BufferedWriter(fwriter);
48             String []tx=text.split("\n");
49             try{
50                 for(int i=0;i<tx.length;i++)
51                 {
52                     out.write(tx[i]);
53                     out.newLine();
54                 }
55             }finally{
56                 out.flush();
57                 out.close();
58                 }
59             }catch(IOException e){
60                 throw new RuntimeException(e);
61                 
62             }
63             
64     }
65 }
    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());
        
        //这里有什么不同的?
        
    }

 

转载于:https://www.cnblogs.com/FakerWang/p/fffakerw.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值