简体   繁体   English

如何使用Groovy读取文件并将其内容存储为变量?

[英]How to read file using groovy and store its contents are variables?

I'm looking for groovy specific way to read file and store its content as different variables. 我正在寻找读取文件并将其内容存储为不同变量的常规方法。 Example of my properties file: 我的属性文件示例:

#Local credentials:
postgresql.url = xxxx.xxxx.xxxx
postgresql.username = xxxxxxx
postgresql.password = xxxxxxx
console.url = xxxxx.xxxx.xxx

At the moment I'm using this java code to read the file and use variables: 目前,我正在使用以下Java代码读取文件并使用变量:

  Properties prop = new Properties();
  InputStream input = null;

  try {
            input = new FileInputStream("config.properties");
            prop.load(input);
            this.postgresqlUser = prop.getProperty("postgresql.username")
            this.postgresqlPass = prop.getProperty("postgresql.password")
            this.postgresqlUrl = prop.getProperty("postgresql.url")
            this.consoleUrl = prop.getProperty("console.url")

        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                }
            }
        }
    }

My colleague recommended to use groovy way to deal with this and mentioned streams but I can't seem to find much information about on how to store data in separate variables, what I know so far is that def text = new FileInputStream("config.properties").getText("UTF-8") could read whole file and store it in one variable, but not separate. 我的同事建议使用常规方法来处理此问题,并提到了流,但是我似乎找不到太多有关如何将数据存储在单独变量中的信息,到目前为止,我所知道的是def text = new FileInputStream("config.properties").getText("UTF-8")可以读取整个文件并将其存储在一个变量中,但不能分开。 Any help would be appreciated 任何帮助,将不胜感激

If you're willing to make your property file keys and class properties abide by a naming convention, then you can apply the property file values quite easily. 如果您愿意使您的属性文件键和类属性遵守命名约定,那么可以很容易地应用属性文件值。 Here's an example: 这是一个例子:

def config = '''
#Local credentials:
postgresql.url = xxxx.xxxx.xxxx
postgresql.username = xxxxxxx
postgresql.password = xxxxxxx
console.url = xxxxx.xxxx.xxx
'''

def props = new Properties().with {
    load(new StringBufferInputStream(config))
    delegate
}

class Foo {
    def postgresqlUsername
    def postgresqlPassword
    def postgresqlUrl
    def consoleUrl

    Foo(Properties props) {
        props.each { key, value ->
            def propertyName = key.replaceAll(/\../) { it[1].toUpperCase() }
            setProperty(propertyName, value)
        }
    }
}

def a = new Foo(props)

assert a.postgresqlUsername == 'xxxxxxx'
assert a.postgresqlPassword == 'xxxxxxx'
assert a.postgresqlUrl == 'xxxx.xxxx.xxxx'
assert a.consoleUrl == 'xxxxx.xxxx.xxx'

In this example, the property keys are converted by dropping the '.' 在此示例中,通过删除“。”来转换属性键。 and capitalizing the following letter. 并大写以下字母。 So postgresql.url becomes postgresqlUrl . 因此postgresql.url成为postgresqlUrl Then it's just a matter for iterating through the keys and calling setProperty() to apply the value. 然后,只需遍历键并调用setProperty()以应用值即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何打开网上的txt文件并将其内容读取为字符串? - How to open a txt file located on the web and read its contents to a string? 读取JSON文件并显示其内容 - read JSON file and display its contents 读取和存储大ASCII文件的内容 - Read and store contents of large ASCII file 如何读取文件内容,写入outputstream以及重新读取并存储在Java内存中的字符串变量中 - how to read contents of file, write to outputstream and the re-read and Store in a string variable in memory in java 如何在不使用数组的情况下读取字符串并将其存储到不同的变量中 - how to read in a string and store into different variables with out using an array 如何使用Java代码批量读取文件内容 - How to read file contents in batches using java code 如何使用Java读取(.bib)文件格式的内容 - How to read the contents of (.bib) file format using Java 如何使用docx4j读取/打印Excel文件的内容? - How to read/print the contents for excel file using docx4j? 如何读取csv文件并在java半透明的摆动窗口中显示其内容? - how to read a csv file and display its contents in java translucent swing window? 如何读取jar中HTML文件的所有内容并将其存储在字符串中 - How do I read all the contents of a HTML file inside of my jar and store it in a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM