现在的位置: 首页 > 综合 > 正文

Study Java in application

2012年10月27日 ⁄ 综合 ⁄ 共 1024字 ⁄ 字号 评论关闭

    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.

抱歉!评论已关闭.