Rok Ivartnik Rok Ivartnik - 1 year ago 41
Java Question

searching from txt file for a specific characters (Java)

I have a big txt. (a dictionary) file which contains about 100k + words ordered like that:

tree trees asderi 12

car cars asdfei 123

mouse mouses dasrkfi 333

plate plates asdegvi 333


(ps. there are no empty rows in between)

what i want to do is to to check the 3th column (asderi in this case at first row) and if there are letters "i" and "e" in this word then copy the first word in this row (tree in this case) to a new txt. file. I don't need a whole solution but maybe and example how to read 3th word and check for it letters and if they are TRUE print the first word in that line out.

Answer Source

When it comes to big data files you want to process line by line rather than reading all of it to your memory you may want to start with this to process the file line by line:

BufferedReader br = new BufferedReader(new FileReader(new File("C:/sample/sample.txt")));
String line;
while ((line = br.readLine()) != null) {
   // process the line.

Once you have the line i bet you will be able to use the common String-methods like .indexOf(.., .substring(..., .split to aquire the data you want (expecially since the source file seems to have well structured data).

So assumed your "columns" are always seperated by a space and there is never a word in a column containing a space nor is there never a column missing you could catch the columns using .split like this:

// this will be the current line of the file 
String s = "tree trees asderi 12";

String[] fragments = s.split(" ");

String thirdColumn = fragments[2];
boolean hasI = thirdColumn.contains("i");

String firstColumn = fragments[0];

System.out.println("Fragment: "+thirdColumn+" contains i: "+hasI+" thats why i want the first fragment: "+firstColumn);

But in the end you will have to try around a bit and play with the String-methods to get it together especially for all special cases this file probably will bring up ;)

You may update your "question" with some source you managed to write with this hints and then ask again if you get stuck.