如何在java中读取String(文件)到数组

时间:2010-12-02 07:40:04

标签: java file-io

假设有一个名为SUN.txt的文件

文件包含:a,b,dd,ss,

我想根据文件中的属性数量制作动态数组。 如果逗号是逗号之后的字符,那么数组将是0-4,即长度为5。 在上面提到的情况下,没有Char返回0-3长度为4的数组。我想在逗号之后读取NULL。

我该怎么做?

Sundhas

6 个答案:

答案 0 :(得分:4)

你应该考虑

  • 将文件读入字符串
  • 按分隔符','
  • 拆分文件
  • 使用列表添加字符并将列表转换为数组,填写列表时

答案 1 :(得分:3)

正如马库斯所说,你想做这样的事情......

//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,你必须在最后添加它,所以你可以做类似的事情

//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();

答案 2 :(得分:1)

如果您想要简单地打开文件并将内容存储在字符串数组中,那么

1)将文件打开成字符串

2)使用正则表达式“,”http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split(java.lang.String

分割字符串

但我很好奇为什么你不能直接使用String文件?

答案 3 :(得分: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();
}

答案 4 :(得分: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++;
}

我没有测试过这段代码

答案 5 :(得分:0)

使用BufferedReader一次读取文件。

使用split(",", -1)转换为String[]数组,包括除最后一个逗号之外的空字符串作为数组的一部分。

String[]部分加载到List