• Study Java in application


        It's a long time from my last Java program, for I usually work using C/C++/Objective-C. In this week, I decide to deal with my data from experiment. There are so many data files and it is a depressing work to turn them into a usable format. So I decide to review Java and write some small programs to help me. At first, I try to rename my data files in order to make them simple.

        Data files are in "data" directory with names like "《2006_6》数据.XLS". And I just want them like "2006_6.xls".

     To achieve my point, Regular Expressions and File operations are used. Regular expressions are used for getting new file names from old ones.

    1 import java.io.File;
    2 import java.util.regex.*;
    3
    4 //Get Old file names
    5 File dir = new File("data"); //"data" are the directory where I store my data files
    6 String[] files = dir.list();
    7
    8 File old_name = null;
    9 File new_name = null;
    10
    11 Pattern pattern = new Pattern.compile("[0-9]+_[0-9]+");
    12
    13 for(String file : files)
    14 {
    15 Matcher matcher = pattern.matcher(file);
    16 matcher.find();
    17 old_name = new File(dir, file);
    18 new_name = new File(dir, matcher.group().concat(".xls"));
    19
    20 old_name.renameTo(new_name);
    21 }

    Now, I can batch renaming my data files for next step.

  • 相关阅读:
    Linux下find命令详解
    shell if语句
    目标文件系统映像制作工具mkyaffs2image
    编译内核
    FPS含义
    linux下echo命令详解
    Mssql数据库语句综合
    string 字符串操作
    Lession 15 Good news
    Mysql使作心得(备份,还原,乱码处理)
  • 原文地址:https://www.cnblogs.com/jsxh/p/1940751.html
Copyright © 2020-2023  润新知