Find a line (or line) in a Java txt file

let's say I have a txt file containing:

john dani zack 

the user enters a string, for example "lobster", I want the program to look for this txt file for the string "omar", if it does not exist, just display "does not exist".

I tried the String.endsWith () or String.startsWith () function, but of course it displays "does not exist" 3 times.

I started java just 3 weeks ago, so I'm a complete newbie ... please bear with me. thank.

+9
java string file
Dec 6 '13 at 7:21
source share
4 answers

Just read this text file and put each word in a List , and you can check if this List contains your word.

You can use Scanner scanner=new Scanner("FileNameWithPath"); to read the file, and you can try the following to add words to the List .

  List<String> list=new ArrayList<>(); while(scanner.hasNextLine()){ list.add(scanner.nextLine()); } 

Then check if your word is there or not

 if(list.contains("yourWord")){ // found. }else{ // not found } 

By the way, you can search directly in the file.

 while(scanner.hasNextLine()){ if("yourWord".equals(scanner.nextLine().trim())){ // found break; }else{ // not found } } 
+4
Dec 06 '13 at 7:22
source share

use String.contains(your search String) instead of String.endsWith() or String.startsWith()

eg,

  str.contains("omar"); 
+1
Dec 06 '13 at 7:22
source share

You can go the other way. Instead of printing “does not exist”, printing “exists” if a match is found while moving the file and break; If the entire file is moved and no match is found, then continue and show 'does not exist.

Also, use String.contains() instead of str.startsWith() or str.endsWith() . Contains the check will look for a match in the entire line, and not just at the beginning or at the end.

Hope this makes sense.

+1
Dec 06 '13 at 7:24
source share

Read the contents of the text file: http://www.javapractices.com/topic/TopicAction.do?Id=42

And after that, just use the textData.contains(user_input); method textData.contains(user_input); where textData is the data read from the file and user_input is the string the user is looking for

UPDATE

 public static StringBuilder readFile(String path) { // Assumes that a file article.rss is available on the SD card File file = new File(path); StringBuilder builder = new StringBuilder(); if (!file.exists()) { throw new RuntimeException("File not found"); } BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String line; while ((line = reader.readLine()) != null) { builder.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return builder; } 

This method returns a StringBuilder created from the data you read from the text file specified as the parameter.

You can see if the user input line is in the file as follows:

 int index = readFile(filePath).indexOf(user_input); if ( index > -1 ) System.out.println("exists"); 
0
Dec 6 '13 at 7:25
source share



All Articles