开发者

how to search for string in file

开发者 https://www.devze.com 2023-04-07 03:58 出处:网络
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

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");
0

精彩评论

暂无评论...
验证码 换一张
取 消