• 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);
            }
            
             
             
            
        }
        
        
        
        
    }

    文件结构

     输出

  • 相关阅读:
    Spark 内核架构+宽依赖与窄依赖+基于Yarn的两种提交模式
    Spark RDD高级编程:基于排序机制的wordcount程序+二次排序+topn
    Spark RDD持久化原理+共享变量原理(Broadcast Variable和Accumulator)
    Spark RDD工作原理详解+RDD JAVA API编程
    Spark 程序设计详解
    剑指offer 39.知识迁移能力 平衡二叉树
    剑指offer 38.知识迁移能力 二叉树的深度
    ElasticSearch 倒排索引原理+document写入流程+数据恢复
    剑指offer 37.知识迁移能力 数字在排序数组中出现的次数
    剑指offer 36.时间空间效率的平衡 两个链表的第一个公共结点
  • 原文地址:https://www.cnblogs.com/kwaitfort/p/9152387.html
Copyright © 2020-2023  润新知