JAVA截取文本特定字符串

需求:给定一个文本,包含若干生物蛋白质信息字符串,要求截取特定信息字符串。

方法:利用JAVA语言的String类来操作。

关键代码:

//流程:1、读入文本数据;2、判断是否包含特定字符串信息;3、截取特定字符串;
//4、去除空格、制表符等无关字符;5、写回到目标文本
String str.contains("……");
str.indexOf(……);
//获取系统换行符:
System.getProperty("line.separator")

具体完整代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class subString {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            BufferedReader br = new BufferedReader(new FileReader("1.txt"));
            BufferedWriter wr = new BufferedWriter(new FileWriter("3.txt"));
            String line = null;
            String str="";
            while((line = br.readLine()) != null)
            {
                str += line;
                if(str.contains("/translation") && str.contains("/product")){
                    int preIndex = str.indexOf("/translation");
                    int endIndex = str.indexOf("/product");
                    String strObject = str.substring(preIndex+13, endIndex).trim();

                    Pattern p = Pattern.compile("\\s*|\t|\r|\n");
                    Matcher m = p.matcher(strObject);
                    String dest = m.replaceAll("");
                    wr.write(">hypothetical protein"+System.getProperty("line.separator")+dest);
                    //wr.write("\n\r");
                    wr.write(System.getProperty("line.separator"));

                    str = null;
                    strObject = null;
                }
            }

            wr.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            System.out.println("not found file 1.txt");
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("read error");
            e.printStackTrace();
        }
        System.out.println("end");
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值