[英]Saving huge file in a string JAVA
我正在尝试将FASTA文件读入Java中的字符串中。 我的代码可以很好地处理小文件,但是当我选择包含500万个字符的真实FASTA文件时,可以使用此字符串, 程序就会卡住 。 卡住=我看不到任何输出,程序变成黑屏。
public static String ReadFastaFile(File file) throws IOException{
String seq="";
try(Scanner scanner = new Scanner(new File(file.getPath()))) {
while ( scanner.hasNextLine() ) {
String line = scanner.nextLine();
seq+=line;
// process line here.
}
}
return seq;
}
尝试使用StringBuilder处理大量文本数据:
public static String ReadFastaFile( File file ) throws IOException {
StringBuilder seq = new StringBuilder();
try( Scanner scanner = new Scanner( file ) ) {
while ( scanner.hasNextLine() ) {
String line = scanner.nextLine();
seq.append( line );
// process line here.
}
}
return seq.toString();
}
我会尝试使用BufferedReader读取文件,如下所示:
public static String readFastaFile(File file) throws IOException {
String seq="";
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// process line here.
}
}
return seq;
}
而且还可以像davidbuzatto所说的那样与StringBuilder连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.