zoukankan      html  css  js  c++  java
  • java编程思想 TextFile IO工具类

    package io;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    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
        public static String read(String fileName){
            StringBuilder sb  =new StringBuilder();
            try {
                BufferedReader in = new BufferedReader(
                                        new FileReader(
                                            new File(fileName).getAbsoluteFile()));
                try {
                    String s ;
                    //BufferedReader.readLine()  如果已到达流末尾,则返回 null 
                    while ( (s = in.readLine())!= null ) {
                         sb.append(s);
                         sb.append("
    ");
                         //要自己加上回车
                    }
                } finally {
                    in.close();
                }
                
                
            } catch (IOException e) {
                throw new RuntimeException();
            }
            return sb.toString();    
        }//read
        
        
        
        //往文件写入 text
        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();
            }
        }//write
        
        
        //构造器
        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, "
    ");//以换行号切分
        }
        
        //复制文件 写入到   文件
        public void write(String fileName){
            try {
                PrintWriter out = new PrintWriter(new File(fileName).getAbsoluteFile());
                try {
                    
                    for (String item : this) {
                        out.print(item);
                    }
                } finally {
                     out.close();
                }
                 
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            }
        }//write
        
        
        public static void main(String[] args) {
            String pa = "D:\dev2\workspace\seehope\bigwork\src\com\ColorChoseListener.java";
            String path2  = "D:\dev2\workspace\seehope\bigwork\src\io\TextFile.java";
            String file  = read(pa);
            write("test.txt", file);
            TextFile text  = new TextFile("test.txt" );
            //以换行号切分
            
            TreeSet<String> words = new TreeSet<String>(new TextFile("src\io\TextFile.java" , "\W+"));
            /*TreeSet<String> words = new TreeSet<String>(new TextFile(path2, "\W+"));
            非词符号切分  ,
            1. 写完整路径  D:\xx\xx\xx\bigwork\src\io\TextFile.java
            2.相对于此项目(bigwork)的路径(srcioxxx.java)
            */
            
            
            // 字母开头的 码值小于  H 
            for (String string : words.headSet("H")) {
                System.out.println(string);
            }
            
             
             
            
        }
        
        
        
        
    }

    文件结构

     输出

  • 相关阅读:
    Vue.nextTick()的介绍和使用场景
    JS实现数据双向绑定
    JS对象的可枚举属性和不可枚举属性
    JS中对象转数组方法总结
    Vmware虚拟机安装XP系统
    javascript和c#的深度拷贝的一种通用方法
    c# SQLServer导入大批量数据
    PowerDesigner逆向工程,从SQL Server数据库生成Physical Model -----数据源方式
    虚拟机和主机ping不通,SQL Server无法远程连接的解决方法
    c#生成试卷。。。
  • 原文地址:https://www.cnblogs.com/kwaitfort/p/9152387.html
Copyright © 2011-2022 走看看