2017-02-25 104 views
0

我試圖將浮點數的數組列表存儲到文件中。
我試着使用DataOutputStream來寫入文件,它工作得很好,只是數據不是以可讀的格式寫入的。
我知道我必須使用打印編寫器以文本格式寫入數據,但不支持寫入浮點。將浮點數組寫入文件

PrintWriter writer = new PrintWriter("filename.txt"); 
writer.write(naiveNearestPoints(points.get(i))); //Gives an error 
writer.println(naiveNearestPoints(points.get(i)));//**updated** correct way to do it 

有沒有我可以寫浮動指向文件的可讀格式的任何其他方式?

+0

的問題是,只有不合適的,因爲你沒有代碼。請[編輯] –

+0

http://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it-in-java Literally LMGTFY –

+0

嘗試PrintWriter這是設計爲文本。 DataOutputStream專爲二進制數據而設計。 –

回答

0

這是什麼意思?

它不支持寫入浮點

PrintWriter可以,事實上,print(float)print(double)


如果不爲你工作,那麼如果你能System.out.print一個浮點,通過將System.out重定向到File,您幾乎可以輕鬆地寫入文件。

例如

File out = new File("out.txt"); 
try { 
    System.setOut(new PrintStream(out)); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

// loops over list, print with System.out.print 
+0

我試圖做這樣的事情PrintWriter作家=新的PrintWriter(「NaiveSolution.txt」); writer.write(naiveNearestPoints(points.get(I)));而且,因爲這給了我一個錯誤,所以我認爲我不能使用PrintWriter寫浮點。但現在我明白了,謝謝! – smriti

+0

您沒有在問題中顯示代碼。我的回答只是寫入文件的一個例子,在Java中有幾種方法可以執行 –

+0

我的壞處,我應該這樣做。對不起,再次感謝! – smriti