-4

I want to write the ArrayList<Double> array into the file in such a way that when I double click on the file then the file opens and the user can read the data.

I have tried the DataOutputStream & RandomAccessFile; both works fine but when I double click on the file it shows data which is not in readable form.

I tried this:

ArrayList<Double> larr=new ArrayList<Double>();
larr.add(5.66);
larr.add(7.89);
try{
    FileOutputStream fos = new FileOutputStream("out.txt");
    DataOutputStream dos =  new DataOutputStream(fos);  
    for(Double d:larr)
        dos.writeDouble(d); 
    dos.close(); 
} catch(Exception ex) {
    ex.printStackTrace();
}

But now the case is that when I open the file out.txt by double clicking on it. It comes in non-readable form.

7
  • 3
    @Lok - use a PrintWriter .... specifically PrintWriter.print(double) Commented Jun 19, 2016 at 1:58
  • @ray I tried that,but it is of no use; as in Files.write it is accepting a char sequence. Commented Jun 19, 2016 at 2:00
  • 1
    @Lok: You'd get a lot more help if you had followed the basic rules of posting questions. For example, you should've provided a Minimal-Complete-Verifiable-Example. You need to provide code for others to look at if you expect any reasonable amount of help and also show that you've actually tried researching something rather than asking right away after getting stuck. Commented Jun 19, 2016 at 2:04
  • @Stephan I tried PrintWriter.print(double) but don't you think it will print only on console as my file remains empty. Commented Jun 19, 2016 at 2:07
  • @downshift but I also want that when I open the file by double clicking on it, then it should come in human readable form. Commented Jun 19, 2016 at 2:14

2 Answers 2

0

I would use a PrintWriter to get human readable values to out.txt (and I would specify the parent folder, personally; I like the user's home directory). Also, I would prefer a try-with-resources close and a method. Something like,

public static void writeList(List<Double> al) {
    File f = new File(System.getProperty("user.home"), "out.txt");
    try (PrintWriter pw = new PrintWriter(f)) {
        for (Double d : al) {
            pw.println(d);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

Also, you could declare and initialize larr1 in one line like

List<Double> larr = new ArrayList<>(Arrays.asList(5.66, 7.89));

1And please program to the List interface.

Sign up to request clarification or add additional context in comments.

Comments

0

It's because the I/O streams you said you tried to use (DataOutputStream and RandomAccessFile) are treating the numbers as binary data, rather than text. You should use a PrintStream.

Example:

PrintStream ps = new PrintStream(new File(filePath));
ps.println(5.25);
ps.close(); // Be sure to close the stream when you're done with saving the numbers

Noticed something familiar with ps.println(5.25)? System.out.println(5.25) does exactly the same thing to the console.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.