【问题标题】:how to search for string in file如何在文件中搜索字符串
【发布时间】:2023-03-17 04:58:01
【问题描述】:

如何在 java 的文本文件中搜索字符串? 它必须在文本文件中还是可以读取 .ini 或其他文件类型 例子

sound = off
opengl = on
openal = off

我尝试在谷歌上搜索它,但我真的找不到任何东西。 提前致谢

【问题讨论】:

标签: java string file


【解决方案1】:

您似乎想使用Properties 文件。

您可以使用Properties.load(String path); 加载一个

【讨论】:

    【解决方案2】:

    如果您的文件是 properties 文件或 ini 文件 (key = value),您可能需要使用 Apache Commons Configuration API。它比 JDK 中的 Properties 类要好得多。

    【讨论】:

      【解决方案3】:

      关于这些典型问题的信息有很多。

      这里有两个简单的例子:

      简而言之,将文件加载到Properties 对象中很容易,例如,在您的情况下,获取example.properties 文件中的sound 值:

      Properties props = new Properties();
      props.load(new FileInputStream("example.properties"));
      String isOnOrOff = props.getProperty("sound");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-07
        • 2011-06-23
        • 1970-01-01
        • 2011-02-04
        • 2021-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多