1

How could I search for string in text file in java? Would it have to be in a text file or could it read a .ini or some other file type example

sound = off
opengl = on
openal = off

I tried searching for it on google but I couldnt really find anything. Thanks in advance

Matthewj
  • 1,932
  • 6
  • 26
  • 37
  • You can process any file as if it contains text (.ini files happen to actually contain text). Possible duplicate of [Finding line number of a word in a text file using java](http://stackoverflow.com/questions/7548519/finding-line-number-of-a-word-in-a-text-file-using-java) – Ted Hopp Sep 25 '11 at 21:06

3 Answers3

4

It looks like you want to use a Properties file.

You can load one using Properties.load(String path);

Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
2

If your file is a properties file or an ini file (key = value), you may want to use the Apache Commons Configuration API. It is much better than the Properties class from the JDK.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
1

There are tons of information with those typical of questions.

Here you have two easy examples:

In short it is easy to load a file into a Properties object, for example to obtain, in your case, the sound value in a example.properties file:

Properties props = new Properties();
props.load(new FileInputStream("example.properties"));
String isOnOrOff = props.getProperty("sound");
Nacho Cougil
  • 552
  • 6
  • 15