Study Java in application

來源:互聯網
上載者:User

    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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.