Sundhas
Sundhas

Reputation: 605

How to read a String (file) to array in java

Suppose there is a file named as SUN.txt

File contains : a,b,dd,ss,

I want to make dynamic array depending upon the number of attributes in file. If ther is a char after comma then array will be of 0-4 i.e of length 5. In the above mentioned case there is no Char which returns 0-3 Array of length 4. I want to read the NULL after comma too.

How do i do that?

Sundhas

Upvotes: 3

Views: 17147

Answers (6)

Camilo Díaz Repka
Camilo Díaz Repka

Reputation: 4815

Read the file, using BufferedReader, one line at the time.

Use split(",", -1) to convert to an array of String[] including also empty strings beyond the last comma as part of your array.

Load the String[] parts into a List.

Upvotes: 0

Brendon Randall
Brendon Randall

Reputation: 1446

As Markus said, you want to do something like this..

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

I do not quite understand why you would want the NULL after the comma? I am assuming that you mean after the last comma you would like that to be null in your array? I do not quite see the point in that but that is not what the question is.

If that is the case you wont read in a NULL, if after the comma there was a space, you could read that in.

If you would like a NULL you would have to add it in yourself at the end so you could do something like

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

Upvotes: 3

chinni776
chinni776

Reputation: 165

its seems like a CSV file something like this will work assuming it has 5 lines and 5 values

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++;
}

i havent tested this code

Upvotes: 0

Andreas Dolk
Andreas Dolk

Reputation: 114757

For your datatructure, use a list of arrays. Each list entry is a line of your textfile, each entry is an array that holds the comma separated values:

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

(assuming, your file contains 1..n lines of comma separated values)


You may want to have it like this:

"a,b,c,d,"  -> {"a", "b", "c", "d", null}

Here's a suggestion how to solve that problem:

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

Upvotes: 0

Jason Rogers
Jason Rogers

Reputation: 19344

Well if you want want to simply open the file and store the content in a array of string then

1) open the file into a string

2) split the string using a regex "," http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split(java.lang.String)

but I'm curious why you can't use a String file directly ?

Upvotes: 1

Markus Lausberg
Markus Lausberg

Reputation: 12257

You should think about

  • Reading the file into a String
  • Splitting the file by separator ','
  • Using a list for adding the characters and convert the list to an array, when the list is filled

Upvotes: 4

Related Questions