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






















































主類實現:
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();
}
}
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:



























排序后要求lrc:















































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