2013-01-21 135 views
0

我使用此代码从pdb网站下载fasta序列文件。 pdb id是字符串protid。下载一个Fasta文件并将其写入文本文件

import java.io.*; 
import java.net.URL; 
import java.util.Scanner; 

public class Trialoffile 
{ 

    public static void main(String[] args){ 

     InputStream url; 

     String protID="2ly4"; 


     try{ 


       url = new URL("http://www.rcsb.org/pdb/download/downloadFile.do?fileFormat=FASTA&compression=NO&structureId="+protID).openStream(); 
       Scanner fasta = new Scanner(url); 

       BufferedWriter bw= new BufferedWriter(new FileWriter(protID+".txt", true)); 




       //output file is prepared. 

       while(fasta.hasNextLine()){ 

       bw.write(fasta.nextLine()+"\n"); 

       } 
       } 





     catch (Exception e) 
     { 
      System.err.println("File input error on:"+protID); 
     } 
    } 

} 

我没有收到错误,但写入的文件是0字节。我试图从同一网站下载另一种格式的文件,并没有问题。

回答

0

当你完成后,不要忘记冲洗和关闭作家!

bw.flush(); 
bw.close(); 
0

试着在最后关闭文件。

while(fasta.hasNextLine()){ 

     bw.write(fasta.nextLine()+"\n"); 

} 
bw.close(); 
相关问题