-1
    package aplikacja;
    import java.io.*;

    public class Plik {
        double wymiary[] =  {10, 23, 4};

        public void zapisz_dane() throws IOException{
           RandomAccessFile raf = new RandomAccessFile("pomiary.csv", "rw");
            for(int i =0; i<wymiary.length; i++){
                raf.writeDouble(wymiary[i]);
            }
            raf.close();
        }

    }

And this:

    package aplikacja;
    import java.io.*;
    import aplikacja.*;

    public class Main {
        public static void main (String []args) throws IOException {
            Plik plik = new Plik();
            plik.zapisz_dane();
        }
    }

When I open CSV files I have chars insides CSV file:

@$@7@

But I would like to have 10, 23 and 4. How to fix it?

It is working for String but not for double or int. Do you know why?

Tom
  • 677
  • 7
  • 21
Adrox
  • 1
  • 1
    Check out the javadoc on RandomAccessFile.writeDouble(double). You are writing out a byte representation of the double values, not String representation. You may want to use a PrintWriter to write to your underlying File. And don't forget to write the commas - its only comma-separated if you separate things with commas! – vsfDawg May 06 '20 at 17:29

2 Answers2

0

A solution using PrintWriter:

package aplikacja;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

public class Plik {

  double[] wymiary = { 10, 23, 4 };

  public void zapiszDane() throws IOException {

    File file = new File("pomiary.csv");

    try (PrintWriter pw = new PrintWriter(file)) {
      for (int i = 0; i < wymiary.length; i++) {
        pw.println(wymiary[i]);
      }
    }

  }
}

pomiary.csv result:

10.0
23.0
4.0

Note: in general there is no way to determine the decimal precision of a double value. If you want decimal precision in Java, you could use BigDecimal.

Tom
  • 677
  • 7
  • 21
-1

You need to specify the charset (usually UTF-8). See here for how to do it. There are more information on that stack overflow thread.

fpezzini
  • 774
  • 1
  • 8
  • 17