java 读取空格分隔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13006729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:07:57  来源:igfitidea点击:

Read space separated values

javafile-ioio

提问by Lolly

I am trying to read space separated value from a configuration file. I am reading a PostgreSQL configuration file pg_hba.conffile, where below is the data the file has:

我正在尝试从配置文件中读取空格分隔值。我正在阅读一个 PostgreSQL 配置文件pg_hba.conf,下面是该文件的数据:

# TYPE  DATABASE    USER        CIDR-ADDRESS          METHOD

# IPv4 local connections:

host    all         all         127.0.0.1/32          md5

I have to get the above values host, all, all, 127.0.0.1/32, md5 and modify and write the modified values back to the same file.

我必须获得上述值 host, all, all, 127.0.0.1/32, md5 并修改并将修改后的值写回同一个文件。

Below is the code I use to read the file:

下面是我用来读取文件的代码:

BufferedReader in = new BufferedReader(new FileReader("pg_hba.conf"));
String str;
str = in.readLine();
while ((str = in.readLine()) != null) {
    System.out.println(str);
}
in.close();

How can I get the those space separated values? Is it possible to write it again with the same format i.e. with same amount of space back to this file?

我怎样才能得到那些空格分隔的值?是否可以使用相同的格式再次写入它,即使用相同的空间回该文件?

Can we use the Propertyclass for this?

我们可以使用这个Property类吗?

回答by Markus

You can use for example the method splitfrom the standard Java class String.

例如,您可以使用从标准 Java 类String拆分的方法。

str.split("\s+");

Another possibility is to use a more "advanced" library like Google Guava, that offers good utility classes such as Splitterfor such tasks:

另一种可能性是使用更“高级”的库,如Google Guava,它提供了很好的实用程序类,例如Splitter用于此类任务:

Splitter.on(' ').split("foo bar")

回答by chrome

You can use String.split() method.

您可以使用 String.split() 方法。

   String[] sa= str.split(" ");

回答by iwein

You should use a csv reading framework with custom separators. You'll be much quicker that way and have less code to maintain.

您应该使用带有自定义分隔符的 csv 阅读框架。这样你会更快,并且需要维护的代码更少。

Open CSVlooks most mature, but you should watch apache commons csvtoo.

Open CSV看起来最成熟,但您也应该观看apache commons csv

回答by Azodious

From your question, I assume that the format of input string won't change. Based on that the code fragments are provided.

从你的问题来看,我假设输入字符串的格式不会改变。在此基础上提供了代码片段。

How can I get the those space separated values ?

我怎样才能得到那些空格分隔的值?

String values[] = new String[9];
bool addingSpace = false;
String value = "";
int ind = 0;
for(int i=0; i<str.length; i++)
{
    char indexValue = str.charAt(i);
    if(Character.isWhitespace(indexValue))
    {
        if(!addingSpace)
        {
            values[ind++] = value;
            value = "";
        }
        addingSpace = true;
    } 
    else if(Character.isLetter(indexValue))
    {
        if(addingSpace)
        {
            values[ind++] = value;
            value = "";
        }
        addingSpace = false;
    }

    value += indexValue;
}

Is it possible to write it again with the same format i.e. with same amount of space back to this file ?

是否可以使用相同的格式再次写入它,即使用相同的空间量返回到该文件?

String values[] = new String[9];

This array has all the data with the exact no. of spaces in between. When you want to write down to file, you can use same array.

该数组包含具有确切编号的所有数据。之间的空间。当您想写入文件时,您可以使用相同的数组。