上一篇是寫入與讀取的練習,利用相同的方法,我寫了一個把英文歌詞和中文歌詞合在一起的程式,最後會產生一個英中對照的文字檔。這次我選的是 You're gonna go far, kid ,歌詞來源是 https://genius.com/The-offspring-youre-gonna-go-far-kid-lyrics ,我把一些不要的刪掉後,存成文字檔,
把兩個檔案放在 project 的資料夾內,上一個練習是放在 src 資料夾裡面,這次是放與 src 同位置的資料夾,原因是這一次我打算保持主程式的簡潔,所以複雜的流程放在類別裡面了。那為什麼這麼一來文字檔放的位置就不同了呢?我也不曉得,查了好久才發現這個現象。
如果想聽這首歌的話,網址:https://www.youtube.com/watch?v=ql9-82oV2JE
混合歌詞的類別:
/* 混合兩份歌詞 */
// 編寫人:Lancelot Cheng
import java.io.*; // I/O 套件是在使用的類別中引入,而非在主程式中引入。
public class Mix2Lyrics {
String name1,name2,name3;
public Mix2Lyrics(String name1, String name2, String name3){
this.name1 = name1; // 要混合的第一份(英文)歌詞檔名。
this.name2 = name2; // 要混合的第二份(中文)歌詞檔名。
this.name3 = name3; // 混合後的檔名。
}
public void mixThem() throws Exception{ // 要記得加 throws Exception
BufferedReader readEng = new BufferedReader(new FileReader(name1));
BufferedReader readChi = new BufferedReader(new FileReader(name2));
String song1;
String song2;
BufferedWriter writeCom = new BufferedWriter(new FileWriter(name3));
while(((song1 = readEng.readLine()) != null) && ((song2 = readChi.readLine()) != null)) {
System.out.println(song1);
System.out.println(song2);
writeCom.write(song1+"\n");
writeCom.write(song2+"\n");
}
readEng.close();
readChi.close();
writeCom.close();
}
}
主程式:
/* 主程式 */
// 編寫人:Lancelot Cheng
public class Main {
public static void main(String[] args) throws Exception{ // 要記得加 throws Exception
Mix2Lyrics uRGonnaGoFar = new Mix2Lyrics
("You're gonna go far, kid.txt", "你會功成名就,孩子.txt","英中對照.txt");
uRGonnaGoFar.mixThem();
}
}
結果:
Show me how to lie, you're getting better all the time
秀給我看你是如何欺騙眾人,你技藝總是越來越好。
And turning all against the one is an art that's hard to teach
讓眾人群起攻擊一個人,那可是一門難以教授的藝術啊!
Another clever word sets off an unsuspecting herd
用另一個聰明的字眼,鼓動毫無戒心的群眾,
And as you step back into line, a mob jumps to their feet
而當你退回人群之中,一群暴徒躁動了起來。
Now dance, fucker, dance man, he never had a chance
現在跳舞吧!混蛋!跳舞人們啊!那個受害者未曾有過機會。
And no one even knew it was really only you
甚至沒有人知道,真正的兇手就只有你!
And now you steal away, take him out today
現在你逍遙法外,讓在受害者在今日失去一切。
Nice work you did, you're gonna go far, kid
幹得好,孩子!你必將功成名就!
With a thousand lies and a good disguise
撒上千百謊言與偽裝成好人,
Hit 'em right between the eyes, hit 'em right between the eyes
痛擊他們的眉心!痛擊他們的眉心!
When you walk away, nothing more to say
當你一走,什麼都沒說,
See the lightning in your eyes, see 'em running for their lives
看看你眼中的閃電,看看他們為了生存而奔跑。
Slowly out of line, and drifting closer in your sight
我慢慢地置身於外,卻飄近你的視野之中。
So play it out, I'm wide-awake, it's a scene about me
所以玩開來吧!我非常清楚,這次輪到我了!
There's something in your way and now someone is gonna pay
你的路上有阻礙,而且有人必須付出代價,
And if you can't get what you want, well, it's all because of me
而如果你不能得到你所要的,哈!那全是因為我!
Now dance, fucker, dance, man, I never had a chance
現在跳舞吧!混蛋,跳舞吧!老兄!我未曾有過機會。
And no one even knew, it was really only you
甚至沒有人知道,真正的兇手其實只有你!
And now you'll lead the way, show the light of day
現在你引領著道路,展現日子的光明
Nice work you did, you're gonna go far, kid, trust deceived
幹得好,孩子!你必將功成名就。信任被欺騙!
With a thousand lies and a good disguise
撒上千百謊言與偽裝成好人,
Hit 'em right between the eyes, hit 'em right between the eyes
痛擊他們的眉心!痛擊他們的眉心!
When you walk away, nothing more to say
當你一走,什麼都沒說,
See the lightning in your eyes, see 'em running for their lives
看看你眼中的閃電,看看他們為了生存而奔跑。
Now dance, fucker, dance, he never had a chance
現在跳舞吧!混蛋!跳舞!那個受害者未曾有過機會。
And no one even knew, it was really only you
甚至沒有人知道,真正的兇手就是你!
So dance, fucker, dance, I never had a chance
所以跳舞吧!混蛋,跳舞吧!我未曾有過機會。
It was really only you
真正的兇手其實只有你!
With a thousand lies and a good disguise
撒上千百謊言與偽裝成好人,
Hit 'em right between the eyes, hit 'em right between the eyes
痛擊他們的眉心!痛擊他們的眉心!
When you walk away, nothing more to say
當你一走了之,什麼都沒說,
See the lightning in your eyes, see 'em running for their lives
看看你眼中的閃電,看看他們為了生存而奔跑。
Clever alibis, Lord of the Flies
巧妙的不在場證明,蒼蠅王!
Hit 'em right between the eyes, hit 'em right between the eyes
痛擊他們的眉心!痛擊他們的眉心!
When you walk away, nothing more to say
當你一走了之,什麼都沒說,
See the lightning in your eyes, see 'em running for their lives
看看你眼中的閃電,看看他們為了生存而躲避。
Process finished with exit code 0
產生的檔案~~
沒有留言:
張貼留言