如何读取一个文件并生成多个文本输出文件

时间:2015-06-29 14:41:00

标签: java io

我有一个像下面这样的文本文件。我的问题是我想在每个段落中读取我的txt文件,并为java上的每个段落生成单独的新文本文件

>T0646 OR188, , 93 residues
MQEERKKLLEKLEKILDEVTDGAPDEARERIEKLAKDVKDELEEGDAKNMIEKFRDEMEQ
MYKDAPNAVMEQLLEEIEKLLKKAGSLVPRGSY


>T0647 HR5514A, , 157 residues
MTERLSAEQIKEYKGVFEMFDEEGNGEVKTGELEWLMSLLGINPTKSELASMAKDVDRDN
KGFFFNCDGFLALMGVYHEKAQNQESELRAAFRVFDKEGKGYIDWNTLKYVLMNAGEPLN
EVEAEQMMKEADKDGDRTIDYEEFVAMMTGESFKLIQ


>T0648 CpN10, , 102 residues
MAGQAFRKFLPLFDRVLVERSAAETVTKGGIMLPEKSQGKVLQATVVAVGSGSKGKGGEI
QPVSVKVGDKVLLPEYGGTKVVLDDKDYFLFRDGDILGKYVD
public class Test{

    public static void main (String[] args){          
        BufferedReader br=null;
        BufferedWriter bw= null;

        File file = new File("...Desktop\\New folder\\out.txt");
        try {
            String s;
            String text = "";
            br = new BufferedReader(new FileReader(
                    "....Desktop\\New folder\\casp10.txt"));
            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            bw = new BufferedWriter(fw);
            while ((s=br.readLine())!= null) {
                if(s.contains(">")){
                    String line=br.readLine();
                    if(line!=null) {
                        System.out.println(line);
                        bw.write('\n'+ line );
                    }
                }
                bw.write(s +"\n");
            }
        } catch(IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(br!=null) {
                    br.close();
                    bw.close();
                }
            } catch(IOException e) {
                e.printStackTrace();
            } 
        }
    }
}

2 个答案:

答案 0 :(得分:0)

每次要使用其他Writer

打印时都会调用此方法
    public class MySaver{
    private PrintWriter innerWriter;
    public MySaver(Writer writer)
    {
    this.innerWriter = new PrintWriter(writer);
    }

    public void save(String c) throws IOException {
        innerWriter.write(c);
    }

}

或者首先使用StringBuilder创建字符串并添加,然后打印它。 String Builder Doc

答案 1 :(得分:0)

每次看到FileWriter符号时,逻辑就是在while循环中创建一个新的'>'对象。 我使用简单的int文件计数器来命名多个输出文件即。

  

out0.txt,out1.txt,out2.txt等等。

见下面的工作代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriting{

    public static void main (String[] args){          
        BufferedReader br=null;
        BufferedWriter bw= null;

        String outFileName = "D:\\New folder\\out";
        try {
            String s;
            br = new BufferedReader(new FileReader("D:\\New folder\\casp10.txt"));
            int fileCounter = 0;

            while ((s=br.readLine())!= null) {
                if(s.contains(">")){
                    if(bw != null){
                        bw.close();
                    }
                    FileWriter fw = new FileWriter(outFileName + (fileCounter++) + ".txt");
                    bw = new BufferedWriter(fw);
                }
                bw.write(s +"\n");
            }
        } catch(IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(br!=null) {
                    br.close();
                    bw.close();
                }
            } catch(IOException e) {
                e.printStackTrace();
            } 
        }
    }
}