[英]How to fill an ArrayList with reading txt file
我正在處理我的代碼,但我試圖用里面的任何一個 txt 文件填充 ArrayList,但似乎我的代碼不起作用,我不知道為什么,我被困在這里,這是 mi 代碼。
ArrayList<String> codonList = new ArrayList();
String currentCodonString;
int countProtein = 0;
ArrayList<ProteinSequence> proteinSequences = new ArrayList();
//Here we can add each nucleotide in group of three from the text, and add an array
public void addNucleotide(String nuceotide) {
try {
BufferedReader read = new BufferedReader(new FileReader("testSequenceOutput.txt"));
String currentCodonString ="";
while(read.readLine() != null); {
codonList.add(currentCodonString);}
}catch(Exception ex) {}
}
//Here we are going to analyze an array(sequence) and see how much protein
//we are going to found in this array
public int findAllProteinSequences() {
if(codonList.contains("AUG")) {
countProtein++;}
else if(codonList.contains("GUG")) {
countProtein++; }
else if(codonList.contains("UGG")) {
countProtein++;
}
else if(codonList.contains("UAA")) {
countProtein++;
}
else if(codonList.contains("UAG")) {
countProtein++;
}
else if(codonList.contains("UGA")) {
countProtein++;
}
return countProtein;
}
似乎我的代碼不起作用,我不知道為什么,我被困在這里
}catch(Exception ex) {}
嗯,是。 當您捕獲所有異常並完全忽略它們時,就會發生這種情況。
刪除該行。 main
可以(並且應該)被聲明為throws Exception
,並且您的文件讀取方法應該被聲明為throws IOException
,然后任何無法解決的問題都將變得可見。
請注意,通過不關閉緩沖讀取器,您會在此處泄漏資源。 您不能只調用.close()(如果發生異常怎么辦?),在 web 中搜索“java 自動資源管理”以了解如何操作。 或者只使用新的 java.nio.file 文件 API 可以通過一個方法調用將文件讀入行列表。
您的代碼有一些問題:
你不應該這樣做:
catch (Exception ex) {}
它說“抓住每一個可能的例外,不要說或做任何事情” 。 在這種情況下,任何可能的異常都包括FileNotFoundException
new FileReader("testSequenceOutput.txt")
如果找不到文件會拋出。 但它也可能包括意外的事情,例如(假設的) NullPointerException
,這可能是由於codonList
是null
類的事情。
這幾乎同樣糟糕:
catch (IOException ex) {}
它不會壓制所有異常,但仍會壓制打開或讀取文件時可能引發的FileNotFoundException
或其他 IO 異常。 您可能應該告訴用戶(或程序員)找不到該文件......或其他什么。
解決這個問題的更好方法是將addNucleotide
的簽名更改為:
public void addNucleotide(String nuceotide) throws IOException {
...
BufferedReader read = new BufferedReader(
new FileReader("testSequenceOutput.txt"));
// process file
}
然后進一步向上調用堆棧,您應該捕獲IOException
並對其進行處理。 根據上下文,您可以打印用戶友好的錯誤消息或打印堆棧跟蹤。 然后您可以退出應用程序或嘗試采取一些恢復措施; 例如,向用戶詢問不同的文件名。
如果您真的不想為處理異常而煩惱,您可以將main
聲明為throws Exception
或throws IOException
但這將導致您的程序的用戶在文件丟失時看到堆棧跟蹤......或不在你期望的地方。 這很難看,在現實世界中,您會收到錯誤報告。
所以有人說你應該將main
聲明為throws Exception
是(IMO)明顯不好的建議:
這就是您的代碼所說的:
String currentCodonString = "";
while (read.readLine() != null); {
codonList.add(currentCodonString);
}
注意;
在第二行?
那是一句空話。 意思是循環的主體是......什么都沒有。 因此,該代碼的真正含義是:
String currentCodonString = "";
while (read.readLine() != null) {
// do nothing
}
codonList.add(currentCodonString);
簡而言之,您正在讀取文件並丟棄其內容,然后將一個空字符串添加到codonList
。
讓我們修復虛假分號:
String currentCodonString = "";
while (read.readLine() != null) {
codonList.add(currentCodonString);
}
那是對的嗎? 沒有!
(read.readLine() != null)
正在讀取一行並測試它是否為null
。 但它與它剛剛的那條線有什么關系呢? 它把它扔掉! 您最終會得到包含大量空字符串的codonList
。
所以你可能需要做的是:
String currentCodonString = read.readLine();
while (currentCodonString != null) {
codonList.add(currentCodonString);
currentCodonString = read.readLine();
}
這可以重寫為:
String currentCodonString;
while ((currentCodonString = read.readLine()) != null) {
codonList.add(currentCodonString);
}
這是一個常見的習慣用法......但是由於在while
條件中有一個賦值表達式,所以這里發生的事情並不完全清楚。 (除非你理解,否則不要這樣做!)
最后一個問題是您的代碼(可能)泄漏了資源描述符。 當addNucleotide(...)
返回...或因異常終止時,它不會關閉它打開的文件。
如果您經常這樣做,您的應用程序將用完“文件描述符”......然后其他打開文件等的嘗試將失敗。
您可以只調用read.close()
,但這並不處理方法由於任意異常而退出的情況。
你可以使用try {... } finally { read.close() }
但它很麻煩並且有一些尷尬的邊緣情況。
對此的推薦解決方案是:
public void addNucleotide(String nuceotide) throws IOException {
try (BufferedReader read = new BufferedReader(
new FileReader("testSequenceOutput.txt"))) {
// process file
...
}
}
這是利用 Java 7+嘗試資源語法。 本例中的資源是read
中的BufferedReader
實例。 當try
終止(正常或異常)時,將檢查read
。 如果它是非空的,它會被自動關閉,並且任何由close()
產生的異常都會得到適當的處理。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.