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     }
  • 相关阅读:
    大数加法、乘法实现的简单版本
    hdu 4027 Can you answer these queries?
    zoj 1610 Count the Colors
    2018 徐州赛区网赛 G. Trace
    1495 中国好区间 尺取法
    LA 3938 动态最大连续区间 线段树
    51nod 1275 连续子段的差异
    caioj 1172 poj 2823 单调队列过渡题
    数据结构和算法题
    一个通用分页类
  • 原文地址:https://www.cnblogs.com/sunxun001/p/13042462.html
Copyright © 2011-2022 走看看