如何在Java中将字符串(文件)读入数组

3
假设有一个名为SUN.txt的文件
文件内容:a,b,dd,ss,
我想根据文件中属性数量创建动态数组。如果逗号后面是字符,则数组长度为0-4,即长度为5。在上述情况下,没有字符返回0-3长度为4的数组。我也想读取逗号后面的NULL。
我该如何做?
Sundhas

1
我不理解“逗号后面的NULL”这部分。 - Andreas Dolk
6个回答

4

你应该考虑

  • 将文件读入字符串中
  • 按逗号分隔文件
  • 使用列表添加字符并在填充列表时将列表转换为数组

我用逗号将其分割,并将其读入数组中,问题在于该数组少了一个索引。 - Sundhas
@Sundhas,如果你在每行使用String.split(","),你可能会发现需要将-1传递给限制参数,就像这样String.split(",", -1),以便获取最后一个元素为空字符串的情况。请参考我发布的答案。 - Camilo Díaz Repka

3
正如Markus所说,你想要做这样的事情...
//Create a buffred reader so that you can read in the file
    BufferedReader reader = new BufferedReader(new FileReader(new File(
            "\\SUN.txt")));
    //The StringBuffer will be used to create a string if your file has multiple lines
    StringBuffer sb = new StringBuffer();
    String line;

    while((line = reader.readLine())!= null)
    {
        sb.append(line);
    }

    //We now split the line on the "," to get a string array of the values
    String [] store = sb.toString().split(",");

我不太理解为什么您想在逗号后面添加NULL?我假设您是指在数组的最后一个逗号后面添加NULL?我不太明白这样做的意义,但这不是问题所在。

如果是这种情况,您不会读取到NULL,如果逗号后面有空格,您可以读取它。

如果您需要NULL,则需要自己在末尾添加。您可以做如下操作:

//Create a buffred reader so that you can read in the file
    BufferedReader reader = new BufferedReader(new FileReader(new File(
            "\\SUN.txt")));
    //Use an arraylist to store the values including nulls
    ArrayList<String> store = new ArrayList<String>();
    String line;
    while((line = reader.readLine())!= null)
    {
        String [] splitLine = line.split(",");
        for(String x : splitLine)
        {
            store.add(line);
        }
        //This tests to see if the last character of the line is , and will add a null into the array list
        if(line.endsWith(","))
            store.add(null);
    }

    String [] storeWithNull = store.toArray();

1
答案很好,但是能否请您删除"C:\"?这是一个毫无意义的Windows特有用语,与问题无关。也许我之所以这么说是因为现在已经凌晨4点了。 - Dan Rosenstark
抱歉,我从代码中删除了C:\,这只是一个测试的习惯,甚至不知道自己做过这个 :P - Brendon Randall

1

0

使用BufferedReader逐行读取文件。

使用split(",", -1)将其转换为包括最后一个逗号之后的空字符串在内的String[]数组。

String[]部分加载到List中。


0

对于你的数据结构,使用一个数组列表。每个列表条目都是文本文件中的一行,每个条目都是包含逗号分隔值的数组:

List<String[]> data = new ArrayList<String[]>();
String line = readNextLine();  // custom method, to be implemented
while (line != null) {
  data.add(line.split(","));
  line = readNextLine();
}

(假设您的文件包含1..n行逗号分隔值)


你可能想要像这样设置它:
"a,b,c,d,"  -> {"a", "b", "c", "d", null}

这里有一个解决该问题的建议:

List<String[]> data = new ArrayList<String[]>();
String line = readNextLine();  // custom method, to be implemented
while (line != null) {
  String[] values = new String[5];
  String[] pieces = line.split(",");
  for (int i = 0; i<pieces.length; i++)
     values[i] = pieces[i];
  data.add(values);

  line = readNextLine();
}

0

看起来像是 CSV 文件,假设有 5 行和 5 个值,类似这样的东西会起作用

String [][] value = new String [5][5];
File file = new File("SUN.txt");
BufferedReader br  = new BufferedReader(new FileReader(file));
String line = null;
int row = 0;
int col = 0;

while((line = br.readLine()) != null ){ 
StringTokenizer s = new StringTokenizer(line,",");

    while (s.hasMoreTokens()){
    value[row][col] = s.nextToken();
    col++;
}
col = 0;
row++;
}

我还没有测试过这段代码


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接