【问题标题】:Read file and identify a String between parenthesis读取文件并识别括号之间的字符串
【发布时间】:2015-04-13 15:44:38
【问题描述】:

我试图将在最后一组括号“genesym”之间出现的字符串命名。到目前为止,我正在用制表符替换每个括号的最后一次出现。在这些函数之间,我想命名现有的字符串基因符号。

我知道这是一个 Scanner 功能,但这是我知道的唯一方式。

import java.lang.*;
import java.io.*;

public class TESTING
{
    public static void main(String[] args)
    {
        try {
            BufferedReader br = new BufferedReader(new FileReader("human.rna.fna"));
            BufferedWriter bw = new BufferedWriter(new FileWriter("FormattedHumanRNA"));

            String line
            String genesym;

            while ((line = br.readLine()) != null) {
                if (line.startsWith(">")) {
                    // Replaces the last set of parenthesis with a tab character
                    int openbracket =  line.lastIndexOf("(");
                    line = new StringBuilder(line)
                        .replace(openbracket, openbracket + 1, "\t")
                        .toString();


                    **genesym = br.nextString();**


                    // Replaces the last close parenthesis with a tab character
                    int closebracket = line.lastIndexOf(")");
                    line = new StringBuilder(line)
                        .replace(closebracket, closebracket + 1, "\t")
                        .toString();
                } else {
                    line = line.replaceAll ("\n", "");
                }

                bw.write(genesym + " : " + line);
            }
            br.close();
            bw.close();
        } catch(IOException e) {
            e.printStackTrace(System.err);
        }
    }
}

示例:(我的数据比这大得多,大约 100 万行)

输入文件:

>365 (LOC1), long non-coding RNA AGCGTCT

>22 (1*split3**) (FLJ), long RNA AAAATC

>13 (RTV), RNA ATGCG

想要的输出:

LOC1 : >365      LOC1     , long non-coding RNA AGCGTCT

FLJ : >22 (1*split3**)      FLJ     , long RNA  AAAATC

RTV : >13     RTV     ,RNA ATGCG

【问题讨论】:

  • 你的问题不清楚。您能否包含您正在阅读的输入文件以及您希望输出的内容?

标签: java string file


【解决方案1】:
String genesym=line.substring(openbracket,closebracket);

然后替换你要替换的。

【讨论】:

  • Stringgenesym = line.substring( lastIndexOf("("), lastIndexOf(")") );为什么我不能这样做?
  • Stringgenesym = line.substring(line.lastIndexOf("("), line.lastIndexOf(")"));
  • 太棒了!感谢您的帮助。我很感激。
猜你喜欢
  • 2017-11-10
  • 1970-01-01
  • 2022-01-23
  • 2016-03-24
  • 2011-03-26
  • 1970-01-01
  • 2016-08-30
  • 2012-07-15
  • 1970-01-01
相关资源
最近更新 更多