I am currently searching the sd card for any file types .csv and .txt. I am displaying the lines of these files as a toast. I’d like to now display only the lines that contain defined keywords. It seems to me that I should use the RuleBasedCollator but I am not sure about how to implement it.
Is that the correct way I should be doing this or is there another, better solution?
Thanks
Code (I’ve commented on the second if, where my question is):
private void conductScan() {
File[] file = Environment.getExternalStorageDirectory().listFiles();
for (File f : file)
{
if (f.isFile() && f.getPath().endsWith(".xml") || f.getPath().endsWith(".txt")) {
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
if (line ) { //here I want to define if line contains "test" then show the toast"
Toast.makeText(getApplicationContext(),line,Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),"No keyewords found",Toast.LENGTH_LONG).show();
}
}
}
catch (IOException e) {
Toast.makeText(getApplicationContext(),"Error reading file!",Toast.LENGTH_LONG).show();
}
String [] mStrings=text.toString().split("\n");
}
}
}
To check if the line contains a substring you could use the:
public boolean contains(CharSequence s)of the
Stringclass.Not sure if that was the problem. The problem with this would be, that your loop will iterate through the lines really fast, and you won’t be able to see the messages.
You could print those lines to the
Logor into another text file to analize it later.EDIT:
You could change this part: