10月底到深圳來找工作,一直沒有給人念想。然后我就在國際文化大廈宇商網做起了銷售,一邊找著工作。
我想我沒有機會(因為是二流本科),就自己創造機會吧。每搞定一個客戶就想把他當作自己的一個機會。
所以一直跟客戶套近乎。
其中有一個藍天科技的公司,問我能不能寫個程序來整理歌詞。如果行,我可以取得面試機會,還會注冊為我們宇商網的會員。
我花了二個小時,搞定了。
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;


public class LrcTimeFormat implements Comparable<LrcTimeFormat>
{

String str;
// java先執行static體
private int hour;
private int min;
private int sec;


public LrcTimeFormat(String str)
{
this.str = str;
hour = Integer.parseInt(str.substring(1, 3));// subString從第一個位置算,第二個位置不算
min = Integer.parseInt(str.substring(4, 6));// subString從第一個位置算,第二個位置不算
sec = Integer.parseInt(str.substring(7, 9));// subString從第一個位置算,第二個位置不算

}

@Override

public int compareTo(LrcTimeFormat ltf)
{


if (this.hour > ltf.hour)
{
return 1;

} else if (this.hour == ltf.hour)
{

if (this.min > ltf.min)
{
return 1;

} else if (this.min == ltf.min)
{

if (this.sec > ltf.sec)
{
return 1;

} else if (this.sec == this.sec)
{
return 0;

} else
{
return -1;
}

} else
{
return -1;
}

} else
{
return -1;
}

}

@Override

public String toString()
{
return str;
}

}

主類實現:
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class SortLrc {
private File lrcFile = null;
private String lrcDir = "";
private JFileChooser dirChoose = new JFileChooser();
private JFileChooser fileChoose = new JFileChooser();
private JFileChooser chooser = new JFileChooser();
private JButton saveButton = new JButton("保存路徑");
private JButton selectButton = new JButton("選擇lrc文件");
private JLabel saveLable = new JLabel("d:");
private JLabel selectLable = new JLabel("d:");
private JPanel panelButton = new JPanel();
private JPanel panelLabel = new JPanel();
private JPanel panelSort = new JPanel();
private JButton sortButton = new JButton("排序歌詞");
public SortLrc() {
JFrame frame = new JFrame("歌詞文件整理");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 選擇目錄,選擇文件的話注釋掉這行。
dirChoose.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fileChoose.setFileSelectionMode(JFileChooser.FILES_ONLY);
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int retValue = dirChoose.showSaveDialog(saveButton);
if (retValue == JFileChooser.APPROVE_OPTION) {
lrcDir = dirChoose.getSelectedFile().toString();
saveLable.setText(dirChoose.getSelectedFile().toString());
}
}
});
selectButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int retValue = fileChoose.showSaveDialog(selectButton);
if (retValue == JFileChooser.APPROVE_OPTION) {
lrcFile = fileChoose.getSelectedFile();
selectLable
.setText(fileChoose.getSelectedFile().toString());
}
}
});
sortButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
sortLrc();
} catch (IOException iOE) {
System.out.println("整理歌詞失敗");
}
}
});
panelButton.add(selectButton);
panelLabel.add(selectLable);
panelButton.add(saveButton);
panelLabel.add(saveLable);
panelSort.add(sortButton);
panelLabel.setLayout(new GridLayout(2, 2, 20, 20));
panelButton.setLayout(new GridLayout(2, 2, 20, 20));
Container pane = frame.getContentPane();
pane.setLayout(new BorderLayout());
pane.add(panelButton, BorderLayout.WEST);
pane.add(panelLabel, BorderLayout.CENTER);
pane.add(panelSort, BorderLayout.SOUTH);
frame.setPreferredSize(new Dimension(400, 300));
frame.pack();
frame.setLocation(400, 200);
frame.setVisible(true);
}
private void sortLrc() throws IOException {
ArrayList<LrcTimeFormat> sortList = new ArrayList<LrcTimeFormat>();
FileReader fr = new FileReader(lrcFile);
BufferedReader br = new BufferedReader(fr);
StringBuilder sb = new StringBuilder();
String str = "";
while ((str = br.readLine()) != null) {
if (!isTimeLine(str)) {
sb.append(str + "\n");
} else {
speLine(str, sortList);
}
}
Collections.sort(sortList);
for (Iterator<LrcTimeFormat> iterator = sortList.iterator(); iterator
.hasNext();) {
LrcTimeFormat lrcTimeFormat = (LrcTimeFormat) iterator.next();
sb.append(lrcTimeFormat + "\n");
}
File sortFile =new File(lrcDir +"\\"+ lrcFile.getName());
System.out.println(sortFile.getAbsolutePath());
FileWriter fw = new FileWriter(sortFile);
fw.write(sb.toString());
br.close();
fr.close();
fw.close();
}
private static Boolean isTimeLine(String str) {
String timeRege = ".*\\[[\\d]{2}\\:[\\d]{2}\\.[\\d]{2}\\].*";
return str.matches(timeRege);
}
private static void speLine(String str, ArrayList<LrcTimeFormat> list) {
String timeRege = "\\[[\\d]{2}\\:[\\d]{2}\\.[\\d]{2}\\]";
Pattern p = Pattern.compile(timeRege);
Matcher m = p.matcher(str);
String tail = str.replaceAll("\\[.*\\]", "");
while (m.find()) {
String lineUnit = m.group() + tail;
// System.out.println(lineUnit);
list.add(new LrcTimeFormat(lineUnit));
}
}
public static void main(String[] args) {
new SortLrc();
}
}
資源:
排序前lrc:
[00:00.00]光輝歲月
[00:09.00]詞 \ 周治平.何啟弘. 曲 \ 黃家駒. 主唱 \ 黃家駒.
[00:18.00]
[00:28.00]一生要走多遠的路程
[00:32.00]經過多少年
[00:36.00]才能走到終點
[00:41.00]夢想需要多久的時間
[00:45.00]多少血和淚
[00:49.00]才能慢慢實現
[02:09.00][00:54.00]天地間任我展翅高飛
[02:15.00][01:00.00]誰說那是天真的預言
[03:59.00][03:23.00][02:22.00][01:07.00]風中揮舞狂亂的雙手
[04:03.00][03:27.00][02:25.00][01:11.00]寫下燦爛的詩篇
[04:06.00][03:30.00][02:29.00][01:14.00]不管有多么疲倦
[04:12.00][03:36.00][02:35.00][01:20.00]潮來潮往世界多變遷
[04:16.00][03:40.00][02:38.00][01:23.00]迎接光輝歲月
[04:19.00][03:43.00][02:41.00][01:27.00]為它一生奉獻
[01:42.00]一生要走多遠的路程
[01:47.00]經過多少年
[01:50.00]才能走到終點
[01:55.00]孤獨的生活很需要時間
[02:00.00]只要肯期待
[02:03.00]希望不會幻滅
[04:37.00]
[04:45.00]
[04:46.00]本歌詞由網友秋風提供

排序后要求lrc:
[ti:恰似你的溫柔]
[ar:蔡琴]
[al:蔡琴——暢銷金曲專輯第三集]
[by:炫網資訊 Liuxuan.com]
[00:00.00]★杰盛唱片★ ★蔡琴——暢銷金曲專輯★第三集★
[00:06.00]★蔡琴——暢銷金曲★ ●恰似你的溫柔●
[00:12.00]詞曲:梁弘志
[00:24.27]某年某月的某一天
[00:30.38]就像一張破碎的臉
[00:36.53]難以開口道再見
[00:43.16]就讓一切走遠
[00:49.49]這不是件容易的事
[00:55.46]我們卻都沒有哭泣
[01:00.95]讓它淡淡的來
[01:06.89]讓它好好的去
[01:11.77]到如今年復一年
[01:15.41]我不能停止懷念
[01:18.94]懷念你懷念從前
[01:23.79]但愿那海風再起
[01:27.59]只為那浪花的手
[01:31.28]恰似你的溫柔
[01:36.92]music ★蔡琴——暢銷金曲★ ●恰似你的溫柔●
[01:56.00]詞曲:梁弘志
[01:59.17]某年某月的某一天
[02:05.09]就像一張破碎的臉
[02:10.49]難以開口道再見
[02:16.90]就讓一切走遠
[02:23.26]這不是件容易的事
[02:29.25]我們卻都沒有哭泣
[02:34.94]讓它淡淡的來
[02:40.47]讓它好好的去
[02:45.29]到如今年復一年
[02:49.10]我不能停止懷念
[02:52.48]懷念你懷念從前
[02:57.28]但愿那海風再起
[03:00.97]只為那浪花的手
[03:04.74]恰似你的溫柔
[03:12.49]music ★蔡琴——暢銷金曲★ ●恰似你的溫柔●
[03:19.00]詞曲:梁弘志
[03:21.17]到如今年復一年
[03:25.00]我不能停止懷念
[03:28.39]懷念你懷念從前
[03:33.22]但愿那海風再起
[03:36.91]只為那浪花的手
[03:40.66]恰似你的溫柔
[03:56.48]○The End○

復習了一遍java文件操作,Comparable接口等實現。不用查api之前就知道java會為我們提供哪些接口。
實現效果自認為還行。也只能這樣了,因為發給對方過后,他就不理人了。留了句:是金子,就會發光的。
給了我莫大的安慰。