How to read a string (file) into an array in java

Suppose there is a file called SUN.txt

The file contains: a, b, dd, ss,

I want to create a dynamic array depending on the number of attributes in the file. If ther is char after the decimal point, then the array will be 0-4 ie of length 5. In the above case there is no char that returns 0-3 An array of length 4. I want to read NULL after the decimal point too.

How should I do it?

Sundhes

+3
source share
6 answers

You should think about

  • Reading file to line
  • Separation of file with delimiter ','
  • Using a list to add characters and convert the list to an array when the list is populated
+4
source

, - .

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

. , , , :

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

i havent

0

, BufferedReader, .

split(",", -1) String[], .

Download parts String[]to List.

0
source

Source: https://habr.com/ru/post/1778054/


All Articles