Java 如何用读取txt文件填充ArrayList

Java 如何用读取txt文件填充ArrayList,java,arraylist,txt,Java,Arraylist,Txt,我正在编写我的代码,但我试图用一个txt文件中的任何内容填充和排列列表,但似乎我的代码不工作,我不知道为什么,我被困在这里,这是mi代码 ArrayList<String> codonList = new ArrayList(); String currentCodonString; int countProtein = 0; ArrayList<ProteinSequence> proteinSequences = new ArrayList(); //Here w

我正在编写我的代码,但我试图用一个txt文件中的任何内容填充和排列列表,但似乎我的代码不工作,我不知道为什么,我被困在这里,这是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;
    }
ArrayList codonList=new ArrayList();
字符串currentCodonString;
int countProtein=0;
ArrayList蛋白质序列=新的ArrayList();
//在这里,我们可以从文本中以三个为一组添加每个核苷酸,并添加一个数组
公共空间(字符串nuceotide){
试一试{
BufferedReader read=新的BufferedReader(新文件读取器(“testSequenceOutput.txt”);
字符串currentCodonString=“”;
while(read.readLine()!=null){
add(currentCodonString);}
}捕获(例外情况除外){}
}
//这里我们将分析一个数组(序列),看看有多少蛋白质
//我们将在这个数组中找到
public int findAllProteinSequences(){
如果(密码列表包含(“AUG”)){
countProtein++;}
else if(codonList.contains(“GUG”)){
countProtein++;}
else if(密码列表包含(“UGG”)){
countProtein++;
}
else if(codonList.contains(“UAA”)){
countProtein++;
}
else if(密码列表包含(“UAG”)){
countProtein++;
}
else if(密码列表包含(“UGA”)){
countProtein++;
}
返回计数蛋白;
}
似乎我的代码不起作用,我也不知道为什么,我被困在这里了

}捕获(例外情况除外){}

嗯,是的。当您捕获所有异常并完全忽略它们时,就会发生这种情况

拆下那条线
main
可以(也应该)声明为
throws Exception
,您的文件读取方法应该声明为
throws IOException
,然后任何不起作用的内容都将变为可见


请注意,您没有关闭缓冲读取器,这是在泄漏资源。您不能只调用.close()(如果发生异常怎么办?),请在web上搜索“java自动资源管理”以了解如何操作。或者只需使用新的java.nio.file文件API,它可以通过一个方法调用将文件读入行列表。

您的代码有一些问题:

挤压异常 你不应该这样做:

catch (Exception ex) {}
它说“抓住每一个可能的例外,不要说或做任何事情”。在这种情况下,任何可能的异常都包括

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
,并对此采取措施。根据上下文,您可以打印用户友好的错误消息或打印stacktrace。然后您可以退出应用程序或尝试执行一些恢复操作;e、 g.要求用户输入不同的文件名

如果您真的不想处理异常,可以将
main
声明为
throws exception
throws IOException
,但如果文件丢失,这将导致程序用户看到堆栈跟踪。。。或者不在你期望的地方。这很难看,在现实世界中,你会因此收到bug报告

因此,有人说你应该声明
main
,因为
抛出异常
(IMO)显然是个坏建议:

  • 如果你把它交给作业中的评估,你应该会因此失去分数
  • 如果您的同事对您的代码进行了检查,您应该期望在签入时获得-1票
  • 如果你发布了你的代码,你应该期望得到负面的评论
假分号 这就是您的代码所说的:

    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
条件中存在赋值表达式,因此这里发生的事情并不完全明显。(除非你理解,否则不要这样做!)

资源泄漏 最后一个问题是编码(可能)泄漏资源描述符。当
添加核苷酸(…)
返回时。。。或由于异常而终止,则不会关闭打开的文件

如果您经常这样做,您的应用程序将用尽“文件描述符”。。。然后其他尝试打开文件等的操作将失败

您可以只调用
read.close()
,但这不能处理由于任意异常而退出方法的情况

您可以使用
try{…}finally{read.close()}
但是它很麻烦,并且有一些棘手的边缘情况

建议的解决方案
public void addNucleotide(String nuceotide) throws IOException {
    try (BufferedReader read = new BufferedReader(
                  new FileReader("testSequenceOutput.txt"))) {
        // process file
        ...
    }
}