Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 逐行分析文本文件,跳过某些行_Java_Arrays_String_List - Fatal编程技术网

Java 逐行分析文本文件,跳过某些行

Java 逐行分析文本文件,跳过某些行,java,arrays,string,list,Java,Arrays,String,List,我有一个看起来像这样的文件(但要大得多): 我在Java中使用它已经有一段时间了,并且能够用行等构建数组。带有“>”的行通常是一行,但有时可能是2行、3行或更多行。不以“>”开头的行的字符长度相同,但可能有10行、20行、30行或更多行。现在我要创建一个字符串数组,数组中的每个字符串都包含一个不以“>”开头的字符串,如下所示: array element 1 = ABCDEFGHI array element 2 = JKLMONPQR 我觉得我离得很近,但需要一个小小的脚踢才能让我继续前进。

我有一个看起来像这样的文件(但要大得多):

我在Java中使用它已经有一段时间了,并且能够用行等构建数组。带有“>”的行通常是一行,但有时可能是2行、3行或更多行。不以“>”开头的行的字符长度相同,但可能有10行、20行、30行或更多行。现在我要创建一个字符串数组,数组中的每个字符串都包含一个不以“>”开头的字符串,如下所示:

array element 1 = ABCDEFGHI
array element 2 = JKLMONPQR
我觉得我离得很近,但需要一个小小的脚踢才能让我继续前进。我相信这对专业人士来说很容易,但我对Java还是新手

具体问题与我在该董事会上发表的其他帖子有关。这是一个FASTA文件:

>3BHS_BOVIN (P14893) 3 beta-hydroxysteroid
AGWSCLVTGGGGFLGQRIICLLVEEKDLQEIRVLDKVFRPEVREEFSKLQSKIKLTLLEG
DILDEQCLKGACQGTSVVIHTASVIDVRNAVPRETIMNVNVKGTQLLLEACVQASVPVFI
>41_BOVIN (Q9N179) Protein 4.1 
MHCKVSLLDDTVYECVVEKHAKGQDLLKRVCEHLNLLEEDYFGLAIWDNATSKTWLDSAK
EIKKQVRGVPWNFTFNVKFYPPDPAQLTEDITRYYLCLQLRQDIVSGRLPCSFATLALLG
SYTIQSELGDYDPELHGADYVSDFKLAPNQTKELEEKVMELHKSYRSMTPAQADLEFLEN
>5NTD_BOVIN (Q05927) 5'-nucleotidase 
MNPGAARTPALRILPLGALLWPAARPWELTILHTNDVHSRLEQTSEDSSKCVNASRCVGG
VARLATKVHQIRRAEPHVLLLDAGDQYQGTIWFTVYKGTEVAHFMNALGYESMALGNHEF
DNGVEGLIDPLLKEVNFPILSANIKAKGPLASKISGLYSPYKILTVGDEVVGIVGYTSKE
TPFLSNPGTNLVFEDEITALQPEVDKLKTLNVNKIIALGHSGFEVDKLIAQKVKGVDVVV

我最终需要将序列放在它们自己的数组元素中,以便以后可以操作它们。

试试这个。我不想麻烦使用正确的变量名。假设第一行有一个>,它也可以工作。它可能也没有得到优化,但应该让您了解这是如何实现的

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;


public class Parse {
    public static void main(String[] args) throws IOException {
        String lala = ">some text\r\n" + 
                "ABC\r\n" + 
                "DEF\r\n" + 
                "GHI\r\n" + 
                ">some more text\r\n" + 
                "JKL\r\n" + 
                "MNO\r\n" + 
                "PQR";

        ArrayList<String> lines = new ArrayList<String>();

        BufferedReader in = new BufferedReader( new StringReader( lala ) );

        String line;
        while( ( line = in.readLine() ) != null ) {
            lines.add( line );
        }

        ArrayList<String> parsed = new ArrayList<String>();

        for( String s : lines ) {
            if( s.contains(">") ) {
                parsed.add("");
            } else {
                String current = parsed.get( parsed.size() - 1 );
                parsed.set( parsed.size() - 1, current + s );
            }
        }

        for( String s : parsed ) {
            System.out.println( s );
        }
    }

}

另一个有趣的方法是在“in.readLine()”循环中,您可以检查>并在将其推到“line”之前在该字符串的末尾添加一个<。然后,您可以使用正则表达式稍后再提取其他行。

试试这个。我不想麻烦使用正确的变量名。假设第一行有一个>,它也可以工作。它可能也没有得到优化,但应该让您了解这是如何实现的

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;


public class Parse {
    public static void main(String[] args) throws IOException {
        String lala = ">some text\r\n" + 
                "ABC\r\n" + 
                "DEF\r\n" + 
                "GHI\r\n" + 
                ">some more text\r\n" + 
                "JKL\r\n" + 
                "MNO\r\n" + 
                "PQR";

        ArrayList<String> lines = new ArrayList<String>();

        BufferedReader in = new BufferedReader( new StringReader( lala ) );

        String line;
        while( ( line = in.readLine() ) != null ) {
            lines.add( line );
        }

        ArrayList<String> parsed = new ArrayList<String>();

        for( String s : lines ) {
            if( s.contains(">") ) {
                parsed.add("");
            } else {
                String current = parsed.get( parsed.size() - 1 );
                parsed.set( parsed.size() - 1, current + s );
            }
        }

        for( String s : parsed ) {
            System.out.println( s );
        }
    }

}

另一个有趣的方法是在“in.readLine()”循环中,您可以检查>并在将其推到“line”之前在该字符串的末尾添加一个<。然后,您可以使用正则表达式稍后再提取其他行。

跳过以
开头的行很容易

while((line=istream.readLine())!=null){
    if(line.charAt(0)=='>')continue;

    //do normal concat to buffers
}
如果要转到以
>开头的行上的下一个缓冲区,
则需要更多的操作

while((line=istream.readLine())!=null){
    if(line.charAt(0)=='>'){
         //create new buffer and append the current one to the list (check first if current one is not empty)
         continue;
    }

    //do normal concat to buffer
}

跳过以
开头的行很容易

while((line=istream.readLine())!=null){
    if(line.charAt(0)=='>')continue;

    //do normal concat to buffers
}
如果要转到以
>开头的行上的下一个缓冲区,
则需要更多的操作

while((line=istream.readLine())!=null){
    if(line.charAt(0)=='>'){
         //create new buffer and append the current one to the list (check first if current one is not empty)
         continue;
    }

    //do normal concat to buffer
}
像这样的

Array<String> lines    
 //Open the file for reading
    try {    
       BufferedReader br = new BufferedReader(new FileReader(<FileNameGoesHere>));
       while ((thisLine = br.readLine()) != null) { // while loop begins here
         if(thisLine.charAt(0) != '>') {
           lines.add(thisLine);
         }
       } // end while 
     } // end try
     catch (IOException e) {
       System.err.println("Error: " + e);
     }
数组行
//打开文件进行读取
试试{
BufferedReader br=新的BufferedReader(新文件读取器());
while((thisLine=br.readLine())!=null){//while循环从这里开始
if(thisLine.charAt(0)!=“>”){
行。添加(此行);
}
}//结束时
}//结束尝试
捕获(IOE异常){
System.err.println(“错误:+e”);
}
像这样的东西

Array<String> lines    
 //Open the file for reading
    try {    
       BufferedReader br = new BufferedReader(new FileReader(<FileNameGoesHere>));
       while ((thisLine = br.readLine()) != null) { // while loop begins here
         if(thisLine.charAt(0) != '>') {
           lines.add(thisLine);
         }
       } // end while 
     } // end try
     catch (IOException e) {
       System.err.println("Error: " + e);
     }
数组行
//打开文件进行读取
试试{
BufferedReader br=新的BufferedReader(新文件读取器());
while((thisLine=br.readLine())!=null){//while循环从这里开始
if(thisLine.charAt(0)!=“>”){
行。添加(此行);
}
}//结束时
}//结束尝试
捕获(IOE异常){
System.err.println(“错误:+e”);
}

假设您可以迭代这些行:

List<String> array = new ArrayList<String>();
StringBuilder buf = new StringBuilder();
for (String line : lines) {
  if (line.startsWith(">")) {
    if (buf.length() > 0) {
      array.add(buf.toString());
      buf.setLength(0);
    }
  } else {
    buf.append(line);
  }
}
if (buf.length() > 0) { // Add the final text element(s).
  array.add(buf.toString());
}
List array=new ArrayList();
StringBuilder buf=新的StringBuilder();
用于(字符串行:行){
if(第行开始时带(“>”){
如果(buf.length()>0){
add(buf.toString());
buf.设定长度(0);
}
}否则{
buf.追加(行);
}
}
如果(buf.length()>0){//添加最后的文本元素。
add(buf.toString());
}

假设您可以迭代这些行:

List<String> array = new ArrayList<String>();
StringBuilder buf = new StringBuilder();
for (String line : lines) {
  if (line.startsWith(">")) {
    if (buf.length() > 0) {
      array.add(buf.toString());
      buf.setLength(0);
    }
  } else {
    buf.append(line);
  }
}
if (buf.length() > 0) { // Add the final text element(s).
  array.add(buf.toString());
}
List array=new ArrayList();
StringBuilder buf=新的StringBuilder();
用于(字符串行:行){
if(第行开始时带(“>”){
如果(buf.length()>0){
add(buf.toString());
buf.设定长度(0);
}
}否则{
buf.追加(行);
}
}
如果(buf.length()>0){//添加最后的文本元素。
add(buf.toString());
}

具体问题是什么?你知道如何连接字符串吗?您知道如何识别字符串是否以“>”开头吗??我有一个文件中的行数组,我可以确定哪个以“>”开头,是的。。。我也可以连接行,但只选择这些行并将它们作为数组中的元素是我遇到的问题。具体的问题是什么?你知道如何连接字符串吗?您知道如何识别字符串是否以“>”开头吗??我有一个文件中的行数组,我可以确定哪个以“>”开头,是的。。。我也可以连接行,但只选择这些行并将它们作为数组中的元素是我遇到的问题。为什么要先将行添加到数组中,然后再次遍历数组以删除不必要的条目,而不是只添加有用的行?你能做到这一点太复杂了。由于要从列表中删除条目,所以可能要向后退,这稍微有点棘手。我想这是我的偏好。我不同意我的方法比你建议的方法复杂得多。为什么你要先将行添加到数组中,然后再次遍历数组以删除不必要的条目,而不是只添加有用的行?你能做到这一点太复杂了。由于要从列表中删除条目,所以可能要向后退,这稍微有点棘手。我想这是我的偏好。我不同意我的方法比你建议的方法复杂得多,这是一个很好的解决方案。除了一件事,它看起来很紧凑,很有效率。对于测试,因为我使用的是一个有三个条目的文件,如果我打印数组,我只会得到前两个条目,如果我打印buf,我会得到第三个条目。这是一个好的开始,我现在正在与它合作,看看我是否能让它一直工作下去。也许我遗漏了什么?@nicorellius:哦,对了,我忘了刷新循环末尾的缓冲区;查看我的更新解决方案。很好!我觉得有点不对劲。不过你做得很完美;——)