0

I am trying to get data out of a CSV data. However, if I try to read the data so I can use the individual data inside it, it prints extra stuff like:

x����sr��java.util.ArrayListx����a���I��sizexp������w������t��17 mei 2017t��Home - Gastt��4 - 1t��(4 - 0)t��

With this code:

FileInputStream in = openFileInput("savetest13.dat");  
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
List<String[]> resultList = new ArrayList();        

String csvLine;
        while ((csvLine = reader.readLine()) != null){
            String[] row = csvLine.split(",");
            out.println("while gepakt");
            out.println(row);

            date = row[0];
            out.println("date: "+date);
            resultList.add(row);
            txtTest.setText(date);
        }

But whenever I read the file to check what data it contains, I get the exact same data as I put in. But I can't manage to split the data with stuff:

FileInputStream in = openFileInput("savetest13.dat");
ObjectInputStream ois = new ObjectInputStream(in);
List stuff = (List) ois.readObject();

txtTest.setText(String.valueOf(stuff));

[17 mei 2017, Home - Guest, 2 - 1, (2 - 0), ]

I am trying to get them separated into date, names, score1, score2

.
Which of the 2 would be better to use and how can I get the correct output, which I am failing to obtain?

2 Answers2

0

You are not writing CSV to your output file, rather than that your are using standard java serialization ObjectOutputStream#writeObject(...) to create that file. try using a CSV library to write/read data in CSV format (see hier) and before that, start here to learn about CSV because

[17 mei 2017, Home - Guest, 2 - 1, (2 - 0), ]

is not csv, but only the output of toString of the list you are using.

Community
  • 1
  • 1
A4L
  • 17,353
  • 6
  • 49
  • 70
0

Here is an easy way to write the CSV file formatted correctly. This is a simple example which does not take into account the need to escape any commas found in your data.You can open the file created in Excel, Google Sheets, OpenOffice etc. and see that it is formatted correctly.

final String COMMA = ",";
final String NEW_LINE = System.getProperty("line.separator");

ArrayList<String> myRows = new ArrayList<String>();

// add comma delimited rows to ArrayList
myRows.add("date" + COMMA +
           "names" + COMMA +
           "score1" + COMMA +
           "score2"); // etc.

// optional - insert field names into the first row
myRows.add(0, "[Date]" + COMMA +
              "[Names]" + COMMA +
              "[Score1]" + COMMA +
              "[Score2]");


// get a writer
final String fileName = "myFileName.csv";
final String dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
File myDirectory = new File(dirPath);
FileWriter writer = new FileWriter(new File(myDirectory, fileName));

// write the rows to the file
for (String myRow : myRows) {
    writer.append(myRow + NEW_LINE);
}
writer.close();