如果在多个位置的文件中找到String,我想将其写入file2。如果在原始文件OR file2中找到该String,则写入file3。如果String在前三个文件中的任何一个中匹配,则写入file4。
我使用了几个不起作用的BufferedWriters。在这帮忙?我用" fileIAmSearching"?
替换什么import java.io.*;
import java.util.*;
public class SortGeneSym {
public static void main (String [] args) {
try {
BufferedReader br = new BufferedReader(new FileReader( "FormattedHumanRNA" ));
String line;
String genesym;
while ((line= br.readLine() ) != null)
{ String arr[] = line.split( "\t");
genesym = arr[0];
//variable genesym is the first String in line
if(fileIAmsearching.contains(genesym)) {
BufferedWriter bw1 = new BufferedWriter(new FileWriter( "1Occurance" ));
bw1.write (line);
// a match!
break;
}
else if (fileIAmSearching.contains(genesym)) {
BufferedWriter bw2 = new BufferedWriter(new FileWriter( "2Occurance"));
bw2.write (line);
break;
}
else if (fileIamSearching.contains(genesym)) {
BufferedWriter bw3 = new BufferedWriter(new FileWriter( "3Occurance"));
bw3.write (line);
break;
}
else (fileIamSearching.contains(genesym) = null ) {
BufferedWriter bw0 = new BufferedWriter(new FileWriter( "0Occurance"));
bw0.writer (line);
break;
}
}
}
catch (IOException e) {
System.out.println ("file probs dne");
}
}
}
答案 0 :(得分:0)
请参阅String类的API,它包含几种允许您搜索对象字符值的方法。例如,您可以使用BufferedReader从文件中读取每一行,然后使用contains方法搜索每一行(String):
while ( ( line = br.readLine() ) != null ){
if ( line.contains(whatImSearchingFor) ){
//do something
}
}
其他一些评论: