how to search for string in file
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 开发者_C百科example
sound = off
opengl = on
openal = off
I tried searching for it on google but I couldnt really find anything. Thanks in advance
It looks like you want to use a Properties file.
You can load one using Properties.load(String path);
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.
There are tons of information with those typical of questions.
Here you have two easy examples:
- Loading Java Properties Files http://viralpatel.net/blogs/2009/10/loading-java-properties-files.html
- How do I load property settings with the Properties class? http://www.jguru.com/faq/view.jsp?EID=581608
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");
精彩评论