zoukankan      html  css  js  c++  java
  • Java 去txt文件的空格、制表符、空格符

     1     /**
     2      * txt文档去空格
     3      * @param url
     4      */
     5     public void test(String url) {
     6         try {
     7             File srcFile = new File(url);
     8             boolean b = srcFile.exists();
     9             //判断是否是路径是否存在,是否是文件夹
    10             if (b && srcFile.isDirectory()) {
    11                 File[] file = srcFile.listFiles();
    12                 if (file.length == 0) {
    13                     System.out.println("文件夹内不存在文件!");
    14                     System.exit(0);
    15                 }
    16                 ;
    17                 for (int i = 0; i < file.length; i++) {
    18                     //file[i]就是循环出来的文件夹里的文件。然后用下面的方法读它。
    19                     //判断是否是TXT文件
    20                     if (!file[i].getName().endsWith("txt")) {
    21                         System.out.println(file[i].getName() + "不是TXT文件!");
    22                         continue;
    23                     }
    24                     //打开待处理文件,参数是字符串,是个命令
    25                     Runtime.getRuntime().exec("notepad " + file[i].getAbsolutePath());
    26                     String str = null;
    27                     //空格、制表符正则表达式,s匹配任何空白字符,包括空格、制表符、换页符等
    28                     String REGEX = "\s+";
    29                     //读入字节流,并且设置编码为UTF-8
    30                     InputStreamReader stream = new InputStreamReader(new FileInputStream(file[i]), "UTF-8");
    31                     //构造一个字符流的缓存器,存放在控制台输入的字节转换后成的字符
    32                     BufferedReader reader = new BufferedReader(stream);
    33                     //建立将要输出的文件和文件名
    34                     File newFile = new File(file[i].getParent(), "new" + file[i].getName());
    35                     //写入字节流
    36                     OutputStreamWriter outstream = new OutputStreamWriter(new FileOutputStream(newFile), "UTF-8");
    37                     BufferedWriter writer = new BufferedWriter(outstream);
    38                     //创建Pattern对象,处理正则表达式
    39                     Pattern patt = Pattern.compile(REGEX);
    40                     while ((str = reader.readLine()) != null) {
    41                         //先处理每一行的空白字符
    42                         Matcher mat = patt.matcher(str);
    43                         str = mat.replaceAll("");
    44                         //如果不想保留换行符直接写入就好,不用多此一举
    45                         if (str == "") {
    46                             continue;
    47                         } else {
    48                             //如果想保留换行符,可以利用str+"
    " 来在末尾写入换行符
    49                             writer.write(str);
    50                         }
    51                     }
    52                     writer.close();
    53                     reader.close();
    54                     //打开修改后的文档
    55                     Runtime.getRuntime().exec("notepad " + newFile.getAbsolutePath());
    56                 }
    57                 System.out.println("文件修改完成!");
    58             } else {
    59                 System.out.println("文件夹路径不存在或输入的不是文件夹路径!");
    60                 System.exit(0);
    61             }
    62         } catch (IOException e) {
    63             System.out.println(e);
    64         }
    65     }
  • 相关阅读:
    生成类似于MongoDB产生的ObjectId
    链式编程:遇到多个构造器参数(Constructor Parameters)时要考虑用构建器(Builder)
    mysql时间字符串按年/月/天/时分组查询 -- date_format
    根据模板导出excel
    九度 1188 约瑟夫环问题
    快速排序
    Linux 命令
    volatile小记
    线程池ThreadPoolExecutor
    CyclicBarrier、CountDownLatch与Semaphore的小记
  • 原文地址:https://www.cnblogs.com/sunxun001/p/13042462.html
Copyright © 2011-2022 走看看