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.

转载于:https://www.cnblogs.com/jsxh/archive/2011/01/21/1940751.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值